Below is my job.sh File

echo 'Job Started Running' echo 'password' | sudo ./logstash -f /usr/share/logstash/bin/contact-index-logstash.conf echo 'Job Ended' 

Below is my sudo crontab -e

* * * * * bash ~/job.sh > ~/log.log 2>&1 

below is my cron status

> service cron status ● cron.service - Regular background program > processing daemon Loaded: loaded (/lib/systemd/system/cron.service; > enabled; vendor preset: enabled) Active: active (running) since Thu > 2017-11-09 22:09:03 IST; 1h 40min ago > Docs: man:cron(8) Main PID: 869 (cron) > Tasks: 1 Memory: 12.3M > CPU: 1min 40.378s CGroup: / > └─869 /usr/sbin/cron -f > > Nov 09 23:42:01 akshay CRON[6264]: pam_unix(cron:session): session >opened for user root by (uid=0) Nov 09 23:43:01 akshay CRON[6270]: > pam_unix(cron:session): session opened for user root by (uid=0) >Nov 09 23:43:01 akshay CRON[6271]: (root) CMD (bash ~/job.sh > ~/log.log > 2>&1) Nov 09 23:43:01 akshay CRON[6270]: pam_unix(cron:session): > session closed for user root Nov 09 23:44:01 akshay CRON[6293]: > pam_unix(cron:session): session opened for user root by (uid=0) Nov 09 > 23:44:01 akshay CRON[6294]: (root) CMD (bash ~/job.sh > ~/log.log > 2>&1) 

When I execute the sh file directly on shell , it runs without any error but under crontab it is not working..

1

1 Answer

Following the suggestions in @steeldriver's comment here's how it should be.

Your job.sh file (assuming logstash is located in the same location!):

echo 'Job Started Running' ./logstash -f /usr/share/logstash/bin/contact-index-logstash.conf echo 'Job Ended' 

Your root's crontab:

* * * * * /home/username/job.sh > /home/username/log.log 2>&1 

cron runs your script with sh by default, so I removed the unnecessary bash subshell – if you nevertheless need it just add it again.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy