-2

and I am newbie for linux, I am sorry if this question is so dumb. I would like to know to create crontab properly, I create job as below:

* * * * * /usr/src/debug/http/cron_job.sh >/dev/null 2>&1

inside cron_job.sh as below

pidof html
if [ 0 = $? ]
then
 echo running
else 
 cd /usr/src/debug/http/
 ./html
fi

but when I check the running program it show that

2738 ? ... ./html
2748 ? ... ./html
2788 ? ... ./html

its seems duplicate it running the application, is there any wrong with my setting? kindly let me know how to solve this issued?

thank you for your help.

3
  • 2
    Redirecting any potential error messages to /dev/null does not help in troubleshooting
    – HBruijn
    Nov 6, 2018 at 8:30
  • Hi, Can you rephrase your question? or clarify the objective? I think you are trying to check if a service is up or not via cron job and then start it if it is down? Have you tested your script outside cron to make sure it is working the way you wanted it to. Also there are other monitoring and reporting tools that can alert you about services (such as Nagios, Icinga). Maybe better to look at those. Nov 6, 2018 at 14:08
  • Also possibly relevant: Prevent duplicate cron jobs running Nov 7, 2018 at 9:51

1 Answer 1

0

Your cron job starts running with its current working directory set to the home directory of the user that owns the job. When you specify the script to be executed as /usr/src/debug/http/cron_job.sh, that does not cause the working directory to be changed.

So, the pidof html is effectively doing the same thing as pidof $HOME/html. Later in the script it is revealed that the html executable is actually in /usr/src/debug/http/html, so the pidof html command needs the full path to the executable you're testing, so replace it with:

pidof /usr/src/debug/http/html

If /usr/src/debug/http/html is actually a script, you'll also need to add the -x option:

pidof -x /usr/src/debug/http/html

Another way to fix the script would be to simply move the cd /usr/src/debug/http command to become the first line of the cron job script, so that the script will always change into the correct directory, before even trying to test for the existence of the html process.

Not the answer you're looking for? Browse other questions tagged .