![]() To learn more about the crontab command: man crontab Standard and Non-Standard Crontab ValuesĬrontab fields accept numbers as values. If we need to to edit another user’s crontab while using sudo, we should use -u option to specify the crontab owner. Note: Please note that using sudo with crontab -e will edit the root user’s crontab file. You should add them to the root user’s crontab file with the following command: sudo crontab -e Some tasks require super admin privileges. Note We can only edit other users’ crontab files as the root user, or as a user with administrative privileges. To do this, we use the -u option as shown in the following command: crontab -u username -e We might need to edit other users’ crontab files. Instead, we put these cron jobs in the /etc/cron.d directory. It’s not recommended to put system-wide cron jobs in /etc/crontab, as this file might be modified in future system updates. The difference is that the sixth field is not the command, but it is the user we want to run the job as. The syntax in system crontabs ( /etc/crontab) is slightly different than user-level crontabs. Important: When both day of month and day of week have certain values (not an asterisk), it will create an OR condition, meaning both days will be matched. The preceding cron job will run once per day every five days, from 5th to 20th of February plus all Tuesdays of February. Consider the following expression: 0 0 5-20/5 Feb 2 /path/to/command If both day of week and day of month have certain values, the event will run when either field matches the current time. The fifth field specifies the day of the week.Ĭron will execute the command when the minute, hour, month, and either day of month or day of week match the current time. The next two fields specify the day of the month and the month. ![]() The first two fields specify the time ( minute and hour) at which the task will run. To remove the crontab, we use the -r option: crontab -r The preceding command will overwrite the existing crontab file with /path/to/the/file/containing/cronjobs.txt. We can also write our scheduled cron jobs in a file and send its contents to the crontab file with teh following command: crontab /path/to/the/file/containing/cronjobs.txt To list the installed cron jobs belonging to our own user: crontab -l If your command runs as root (sudo) you will not be able to define this crontab from your own user account unless you log in as root. ![]() Note: Each command in the crontab file executes from the perspective of the user who owns the crontab. If everything is set properly, the file will be saved to the spool directory. We can also explicitly choose or change our desired editor for editing the crontab file: export VISUAL =nano crontab -eĪfter we save the file and exit the editor, the crontab will be checked for accuracy. If you haven’t chosen a default editor for the crontab before, you’ll see a selection of installed editors to pick from. The above command will automatically open up the crontab file which belongs to our user. On Ubuntu it’s /var/spool/cron/crontabs while in CentOS it’s /var/spool/cron. Note: The spool directory varies across different distributions of Linux. Instead, we should edit them using the crontab command-line utility. You should not edit these files directly. All user-level crontab files reside in Cron’s spool area. Users can have their own crontab files named after their username as registered in the /etc/passwd file. The first part 0 0 * * * is the cron expression, which specifies the frequency of execution. Crontab does not allow comments on active lines.īelow is an example of a crontab file with just one entry: 0 0 * * * /var/www/sites/db_backup.sh Lines starting with # are considered comments.Īctive lines in a crontab are either the declaration of an environment variable or a cron job. In a crontab file, blank lines or lines starting with #, spaces or tabs will be ignored. Each row specifies when and how often Cron should execute a certain command or script. Each line in the crontab file is called a cron job, which resembles a set of columns separated by a space character. Cron uses a special configuration file called a crontab file, which contain a list of jobs to be done.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |