8

I need the exact code that I would use because I'm not able to figure it out. I use crontab -e to set up a cron job but when I save it, it never works. I'm also just trying to run a file called newmail.sh from the root user.

  • Have you find the answer to your question? – bendaf Jan 23 at 13:21
10

If you wanted the task to run every day at 3:17pm, you would use:

17 15 * * * root newmail.sh

This says "run newmail.sh as root every day of the week, every day of the month, every month at 3:17pm."

minute hour dom month dow user cmd

minute - This controls what minute of the hour the command will run on, and is between '0' and '59'

hour - This controls what hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)

dom - This is the Day of Month, that you want the command run on, e.g. to run a command on the 19th of each month, the dom would be 19.

month This is the month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)

dow - This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. sun).

user - This is the user who runs the command.

cmd - This is the command that you want run. This field may contain multiple words or spaces.

from: http://www.unixgeeks.org/security/newbie/unix/cron-1.html

  • on ubuntu 16 and later the new format is: "m h dom mon dow command" – bendaf Jan 23 at 11:55
5
sudo crontab -e

Add these lines in end of file

*/1 * * * * wget -O /dev/null  project_crone_URL

(Run cron per once in 1 minute)

0 0 * * * wget -O /dev/null  project_crone_URL

(Run cron per once in 24 hours)

save file

Start crontab by this command

sudo /etc/init.d/cron start

Stop crontab by this command

sudo /etc/init.d/cron stop

Restart crontab by this command

sudo /etc/init.d/cron restart
1

If you use crontab -e since Ubuntu 16.0+ will not have the user field, so the correct format is:

17 15 * * * newmail.sh

This says "run newmail.sh every day of the week, every day of the month, every month at 3:17pm."

Other ways dm78's answer is correct and contains the exact description of the format.

Also if something goes wrong and you have a mail system installed it will send you a message. For more info see this answer

If you are interested in logging of cronjobs, you can use this answer.

  • 1
    It depends on which crontab you edit. The crontab -e thingy does not have a user field (as you correctly say) but the file /etc/crontab (the socalled system crontab) and the files below /etc/cron.d do have a user field. – PerlDuck Jan 23 at 18:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.