Friday, November 20, 2015

Cron and Anacron to schedule jobs in Linux


ref https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/

$crontab -e   to edit
$crontab -l   to list
$crontab -l -u username   to list cron jobs of username


anacron

runs commands periodically. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that aren't running 24 hours a day, to control daily, weekly, and monthly jobs that are usually controlled by cron.
qyang@lubuntu-laptop:~$ service anacron start
qyang@lubuntu-laptop:~$ service anacron status
● anacron.service - Run anacron jobs
   Loaded: loaded (/lib/systemd/system/anacron.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2015-11-21 09:19:57 AEDT; 4s ago
 Main PID: 10233 (anacron)
   CGroup: /system.slice/anacron.service
           └─10233 /usr/sbin/anacron -dsq

Nov 21 09:19:57 lubuntu-laptop systemd[1]: Started Run anacron jobs.
Nov 21 09:19:57 lubuntu-laptop systemd[1]: Starting Run anacron jobs...
Nov 21 09:19:57 lubuntu-laptop anacron[10233]: Anacron 2.3 started on 2015-11-21
Nov 21 09:19:57 lubuntu-laptop anacron[10233]: Will run job `cron.daily' in 5 min.
Nov 21 09:19:57 lubuntu-laptop anacron[10233]: Will run job `svnsyncPQAFW.daily' in 15 min.
Nov 21 09:19:57 lubuntu-laptop anacron[10233]: Jobs will be executed sequentially

cron

Sample of downloading data regularly using cron Changing crontab
root@trac ~# cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root    cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#25 6 * * * root test -x /usr/sbin/anacron || ( cd /root/AuctionResults && run-parts --report /root/AuctionResults)
00 12  * * 7 root test -x /usr/sbin/anacron || ( cd /root/AuctionResults && run-parts --report /root/AuctionResults)
#
Run any executable files,eg.runpython under nominated folder AuctionResults
root@trac ~# ls -al ~/AuctionResults
total 852
drwxr-xr-x  2 root root   4096 Nov 15 12:00 .
drwx------ 11 root root   4096 Sep  9 00:41 ..
-rwxr-xr-x  1 root root   1105 Jul 14 13:48 file-retrieve-via-url.py
-rwxr-xr-x  1 root root     39 Jun  9 22:43 runpython
qyang@lubuntu-laptop:~/Svn_Local_Personal/A_001_Notes$ locate cron |grep bin
/usr/bin/crontab
/usr/sbin/anacron
/usr/sbin/cron
/etc/cron.daily/.placeholder
/etc/cron.daily/0anacron
/etc/cron.daily/apport
/etc/cron.daily/apt
/etc/cron.daily/bsdmainutils
/etc/cron.daily/dpkg
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
/etc/cron.daily/mlocate
/etc/cron.daily/ntp
/etc/cron.daily/passwd
/etc/cron.daily/popularity-contest
/etc/cron.daily/update-notifier-common
/etc/cron.hourly/.placeholder
/etc/cron.monthly/.placeholder
/etc/cron.monthly/0anacron
/etc/cron.weekly/.placeholder
/etc/cron.weekly/0anacron
/etc/cron.weekly/fstrim
/etc/cron.weekly/man-db
/etc/cron.weekly/update-notifier-common
/etc/default/anacron
/etc/default/cron
/etc/init/anacron.conf
/etc/init/cron.conf
/etc/init.d/anacron
/etc/init.d/cron

No comments: