Solved Script not running from Cron

Hello, I have that sh script:

Code:
#!/bin/sh
DATE=`date +%Y-%m-%d:%H:%M:%S`

NAME="game"
if ps ax | grep -v grep | grep $NAME > /dev/null
then
    echo "$NAME service running, everything is fine"
else
   echo "$DATE: Restarting $NAME" >> /root/log
   cd /usr/home/game/db && ./game
fi

and it works well. But no matter what I tried, it won't just work via cron.
I tried all these variations, still not loading via crontab:

Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/home:/root
* * * * * /bin/sh /root/restart.sh
* * * * * /root/restart.sh
* * * * * sh /root/restart.sh

Cron Logs:

Code:
Aug 22 09:12:00 FE8 /usr/sbin/cron[94432]: (root) CMD (/root/restart.sh^M)
Aug 22 09:12:00 FE8 /usr/sbin/cron[94433]: (root) CMD (/bin/sh /root/restart.sh^M)
 
Uhm, if you use * * * * * then you're basically starting that script every minute, was that what you intended? You might want to add a specific time stamp in there, see crontab(5).

First: what do you mean with "it doesn't work"? That doesn't really tell me much. Also: how did you set up the cronjob? Which file did you edit or what command did you use?

(edit)

Judging from the logs the script did get started, so it does work. But if the results weren't what you expected... you might also want to check your e-mail, any output generated from cronjobs is sent by e-mail.
 
Yes it is supposed to run every minute.
The cron file is in
Code:
/var/cron/tabs/root

The cron is not working though, as there is nothing in /root/log written, but it does write if I start the script via SSH Terminal (sh restart.sh)

Is that "^M)" not strange in the cron logs, added ad the end to the ".sh"?
 
Ok, so first of all I'd be very careful with (re)starting a game as the root user. Normally that should be set for a regular user account (also because of security reasons). But that's besides the point here.

Just to be sure: you used # crontab -e (as root) and then set up the actual crontab file, right?

What's in your mailbox? So, when logged onto your server what do you get to see if you run mail?

See, there could be more reasons why nothing is written to /root/log. After all: the echo command in the first condition simply writes its message to stdout. And if that runs every minute then you'll be looking at a full mailbox.

Solely basing myself on the output from your cron log I can't help but think that the script is running as it should.

(edit) I agree that the ^M looks out of place there. You might want to double check what you put into the crontab file.
 
If you edit the file directly the job will never run. Use crontab -e. And for all things holy, do NOT run your game as root.
 
Yes it is supposed to run every minute.
The cron file is in
Code:
/var/cron/tabs/root

The cron is not working though, as there is nothing in /root/log written, but it does write if I start the script via SSH Terminal (sh restart.sh)

Is that "^M)" not strange in the cron logs, added ad the end to the ".sh"?
Which editor did you use? That ^M could mean a DOS end-of-line. In that case save the file with Unix EOL.
 
It works all well now, the solution was to create the cron file with
Code:
crontab -l
and not manually through WINSCP as I did.
 
Is there any reason you guys don't recommend to start a game as root user?
It goes even deeper than that: on Unix you really don't want to use root at all, only if you need to. Which is normally pretty much never unless you're actually working on the OS itself (think about performing updates).

Unlike an admin in Windows the root user on Unix is pretty much all powerful. I'm not trying to be overly dramatic here mind you; but one small mistake in an rm command can easily be enough to trash your entire system.

In the end this is all about security and best practice.
 
Back
Top