I created a cronjob in the following way to run a script. The script started a service only if it was down.
Here is the script,
#!/bin/bash
service=influxdb
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
service $service start
fi
I created a cron job like this,
alphauser@AlphaServer:~$ sudo crontab -e
And then added this line
* * * * * bash /home/alphauser/influx-start.sh > /home/alphauser/output-influx-start.txt
I stored the output in a file just to check its output.
The service stopped and now it was the time for cron to show its magic. But it failed to start the service. I saw the output file and this was written in that,
Starting influxdb...
influxdb process was unable to start [ FAILED ]
then I removed this cronjob from by sudo crontab -r
.
I added this line at the end of etc/crontab
file i.e,
* * * * * root bash /home/alphauser/influx-start.sh > /home/alphauser/outputinflux.txt
and it worked. The service started and This was the output,
influxdb is running!!!
I want to know that why it failed with sudo crontab -e
but it worked with etc/crontab
file.
The authentication of sudo cannot be the issue because I added it in the sudo crontab
and by chance if that would have been the case, it would have said You must be root to run this script
.
sudo crontab -e
means that it should be run by root ? – luv.preet Apr 21 '17 at 14:46sudo crontab -e
, and those answers were upvoted. – luv.preet Apr 21 '17 at 14:48service
commands (which you should really move away from using btw - since 16.04 is systemd based) are in/usr/sbin
- which is not in in root's personal crontabPATH
but is added toPATH
in/etc/crontab
– steeldriver Apr 21 '17 at 14:57sudo crontab -e
as root all the time. – Jos Apr 21 '17 at 15:06/etc/crontab
file to thesudo crontab -e
file actually worked. – Terrance Apr 21 '17 at 15:20