Running a script everyday using a cronjob
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.
Solution 1:
If you wanted the task to run every day at 3:17pm, you would use in /etc/crontab
or files in /etc/cron.d
:
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
Solution 2:
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
Solution 3:
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.