I have an sh file called m.sh with the contents:

echo "Cleaning memory"
sync; echo 3 > /proc/sys/vm/drop_caches
echo ""
free -m

echo ""

ps cax | grep java > /dev/null
if [ $? -eq 0 ]; then
  echo "== Server is already running =="
  sh a.sh
  echo "========== STARTING THE SERVER =========="

When I run it with sh m.sh it works perfectly. But I need it to run every 5 minutes, so I am using cron jobs. It needs to work as root/sudo so I did sudo -s, then crontab -e and wrote this in the file:

0,4,9,14,19,24,29,34,39,44,49,54 * * * * /bin/sh /home/<username>/m.sh

I had done some research, and I learned that the cron has different paths than the user, that is why I used those absolute paths, to make sure.

The cron job wasn't running the script every 5 minutes, as it was supposed to.

I also added

* * * * * /bin/echo "Testing123"

to test if cron was working at all, and nothing came up in the console.

How can I make the cron run the script every 5 minutes? I did research online, and tried the solutions, but couldn't make it to work for me. I did service cron start and it said it was already running. I also restarted the service. The permissions are set correctly, I gave all users read, write, and execute permissions just to make sure (I know the permissions are correct).


Regarding your test script -- crontab jobs are not connected to a physical console, so don't expect any output. Try

* * * * * /bin/touch /tmp/foo

... and see whether the /tmp/foo file gets updated. It should.

To get a script running every five minutes, there is a shortcut:

*/5 * * * * /bin/sh /whatever/script/blah.sh

Furthermore, I would edit your script such that it logs its activity. For example,

# ...
echo `date '+%Y-%m-%d %H:%M:%S'` Starting server >> $LOGFILE

Alternatively, you can redirect the output from the crontab file directly:

*/5 * * * * /bin/sh /whatever/script/blah.sh >> /var/log/leoncleaner.log
  • Thanks, I've got it figured out now. I don't know what the problem was before, maybe it was working but I didn't know it. Also, is there a way to make it write the output in the console? – user172548 Jul 4 '13 at 12:15
  • Sort of. Redirect the output of the script to a log file (logfile.name). Then open a console and type tail -f logfile.name. It will read the file logfile.name and update the output when the file grows. – January Jul 4 '13 at 12:16
  • Oh, wait, it isn't fixed. I stopped the server, and waited for the cron job to restart it. In the logs I could see that when it noticed the server had stopped, it said "Starting the server", but it actually didn't. But when I run that script myself, and it said "Starting the server", it did. The script is working apparently, because it works when I run it, but only half works when it is run by cron – user172548 Jul 4 '13 at 12:28
  • OK, but that is another issue. Add the full path to a.sh. – January Jul 4 '13 at 12:29
  • I changed sh a.sh with sh /home/<username>/a.sh but the exact thing still happens – user172548 Jul 4 '13 at 12:33

Try running echo PATH=$PATH > path.tmp. Then add the contents of path.tmp to the beginning of your script. This will provide the same path you have interactively to the script.

You may want to add a bang path to beginning of your scrript. Something like #!/bin/bash should work.

Also make sure your script is executable. Run the command chmod +x m.sh. Then test your script can be run as ./m.sh.

Output of the script should be mailed to the user id of the crontab it is running. In the case of root, ensure you have an alias to a user id you are monitoring for mail in /etc/aliases. Generally, you don't want output from cron scripts unless something fails. Redirect output to a log if you need it.

Your Answer

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