Follow this guide to using crontab in Linux. The following is a list of cron directories:
Open a terminal and copy your shell script ‘script.sh’ into one of the directories above. If you need to run the script hourly, place your script file in the “cron.hourly” folder. For daily, place it inside the “cron.daily” and so forth.
Give the shell script the correct permission. For example, if script is called “script.sh”, set permission as follows:
cd /etc/cron.daily/ chmod 755 script.sh
Add new cron job to crontab:
This opens vi editor for you. Create the cron command using the following syntax:
- The number of minutes after the hour (0 to 59)
- The hour in military time (24 hour) format (0 to 23)
- The day of the month (1 to 31)
- The month (1 to 12)
- The day of the week(0 or 7 is Sun, or use name) - The command to run
An example command would be
0 0 * * * /etc/cron.daily/script.sh. This would mean that the shell script will exactly execute at midnight every night. I personally would use an online generator to learn the syntax.
To save the changes to the crontab that you just made, hit ESC key, and then type :w followed by :q to exit.
# To list existing cron jobs: crontab -l crontab -u username -l # To remove an existing cron job: crontab –e #Delete the line that contains your cron job #Type: Escape - :wq