scheduling processes at crontab - useful for backup jobs /etc/cron.d crontab -l

at - one time running
- uses atd
- use at to add jobs
=============
[root@svn ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-07-04 21:45:50 EEST; 2h 6min ago
 Main PID: 1150 (atd)
   CGroup: /system.slice/atd.service
           └─1150 /usr/sbin/atd -f

Jul 04 21:45:50 svn.localdomain systemd[1]: Started Job spooling tools.
Jul 04 21:45:50 svn.localdomain systemd[1]: Starting Job spooling tools...
[root@svn ~]# 

=============

[root@svn ~]# at 23:58
at> touch /tmp/f22
at> <EOT>CTRL+D
job 3 at Tue Jul  4 23:58:00 2017
[root@svn ~]# ll /var/spool/at/
total 4
-rwx------. 1 root   root   2958 Jul  4 23:56 a00003017d444a

drwx------. 2 daemon daemon    6 Jul  4 23:55 spool
=============

[root@192 ~]#at now + 20 days

============================= crontab
- uses crond service
- started by default and used by different services
- configuration files in different locations
   - Allows rpm's to drop shell scripts in cron
   - Or users to create their own cron jobs

There is a system-wide /etc/crontab file, the /etc/cron.d directory may contain crontab fragments which are also read and actioned by cron. Some Linux distributions (eg, Red Hat) also have /etc/cron.{hourly,daily,weekly,monthly} which are directories, scripts inside which will be executed every hour/day/week/month, with root privilege.


There is a file called cron.deny which will specify which users cannot use cron. The cron.deny file location is system dependent and can be deleted which will allow all users to use cron.



The first 5 fields of the line represent the time(s) when the command should be run. You can use numbers or where applicable day/month names in the time specification.


  • The fields are separated by spaces or tabs.
  • A comma (,) is used to specify a list e.g 1,4,6,8 which means run at 1,4,6,8.
  • Ranges are specified with a dash (-) and may be combined with lists e.g. 1-3,9-12 which means between 1 and 3 then between 9 and 12.
  • The / character can be used to introduce a step e.g. 2/5 which means starting at 2 then every 5 (2,7,12,17,22...). They do not wrap past the end.
  • An asterisk (*) in a field signifies the entire range for that field (e.g. 0-59 for the minute field).
  • Ranges and steps can be combined e.g. */2 signifies starting at the minimum for the relevant field then every 2 e.g. 0 for minutes( 0,2...58), 1 for months (1,3 ... 11) etc.

If your cronjobs stop working, check that your password hasnt expired., since once it has, all cron jobs stop.



Consider the following job which commonly would be explained to "run command every 5 minutes":


*/5 * * * * /path/to/your/command

[svn@svn ~]$ cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs


# Example of job definition:

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed


[svn@svn ~]$ 


crontab -e #opens editor mode for current user and create cron configuration file for this user

0 23 * * 1-5 yum update -y
@reboot script

0 - min
23 - hour
* - day of month
* - month
1-5 - day of week

[root@192 centos]# cat /var/spool/cron/centos
* * * * * logger hello


[svn@svn ~]$ ll /etc/cron.daily/
total 12
-rwx------. 1 root root 219 Nov  6  2016 logrotate
-rwxr-xr-x. 1 root root 618 Mar 17  2014 man-db.cron
-rwx------. 1 root root 208 Nov  4  2016 mlocate
[svn@svn ~]$ 

[svn@svn ~]$ ll /etc/cron
cron.d/       cron.deny     cron.monthly/ cron.weekly/  
cron.daily/   cron.hourly/  crontab     

[root@svn ~]# crontab -l
*/1 * * * * date >> /tmp/file
[root@svn ~]# tail -f /tmp/file 


[root@svn ~]# cat /etc/cron.d/f

*/1 * * * * root date >> /tmp/f

When adding a cron configuration in /etc/cron.d/ or in /etc/crontab you have to add the username in which context the command should run, in your example
* * * * * root /bin/touch /home/me/ding_dong
And just a hint from me: you don't have to start running ls -ltr again and again, just use watch -n 5 "ls -ltr" and it will run the command every 5 seconds (or any other value by replacing 5 with what you want).

[root@svn ~]# cat /var/log/cron
23:41:01 svn CROND[6042]: (root) CMD (date >> /tmp/file)
Jul  4 23:41:01 svn CROND[6041]: (root) CMD (date >> /tmp/f)
Jul  4 23:42:01 svn CROND[6068]: (root) CMD (date >> /tmp/file)
Jul  4 23:42:01 svn CROND[6069]: (root) CMD (date >> /tmp/f)


Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all