Ask Your Question
1

scheduling a daily script in anacron

asked 2017-03-03 01:34:40 -0500

wolfv gravatar image

updated 2017-03-05 01:13:33 -0500

I want to schedule a daily backup script on my home computer. But not having any luck with anacron. I wrote this one-line script to test anacron and placed it in /etc/cron.daily:

$ cat /etc/cron.daily/anacron_test.sh
echo "$(date) in daily" >> /home/wolfv/anacron_test.log

The script appends date-time into anacron_test.log. It works as intended when called from the terminal:

$ /etc/cron.daily/anacron_test.sh
$ cat /home/wolfv/anacron_test.log
Thu Mar  2 23:48:44 MST 2017 in daily

But anacron does not seem to be running the anacron_ test.sh. Running anacron now did not append to anacron_ test.log:

$ anacron -n
$ cat /home/wolfv/anacron_test.log
Thu Mar  2 23:48:44 MST 2017 in daily

The crond service is running:

$ systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2017-02-27 21:44:32 MST; 3 days ago
 Main PID: 956 (crond)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/crond.service
           └─956 /usr/sbin/crond -n

Mar 02 13:01:01 localhost.localdomain CROND[9381]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 14:01:01 localhost.localdomain CROND[11901]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 15:01:01 localhost.localdomain CROND[14090]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 16:01:01 localhost.localdomain CROND[17730]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 17:01:01 localhost.localdomain CROND[20443]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 19:01:01 localhost.localdomain CROND[24950]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 20:01:01 localhost.localdomain CROND[27066]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 21:01:01 localhost.localdomain CROND[29327]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 22:01:01 localhost.localdomain CROND[32004]: (root) CMD (run-parts /etc/cron.hourly)
Mar 02 23:01:01 localhost.localdomain CROND[2220]: (root) CMD (run-parts /etc/cron.hourly)

The system log says anacrontestid is running:

$ journalctl /usr/sbin/anacron
Mar 04 00:01:01 localhost.localdomain anacron[10346]: Anacron started on 2017-03-04
Mar 04 09:01:01 localhost.localdomain anacron[11023]: Anacron started on 2017-03-04
Mar 04 09:01:01 localhost.localdomain anacron[11023]: Will run job `cron.daily' in 47 min.
Mar 04 09:01:01 localhost.localdomain anacron[11023]: Will run job `anacron_test_id' in 50 min.
Mar 04 09:01:01 localhost.localdomain anacron[11023]: Jobs will be executed sequentially
Mar 04 09:48:01 localhost.localdomain anacron[11023]: Job `cron.daily' started
Mar 04 09:48:01 localhost.localdomain anacron[11023]: Job `cron.daily' terminated
Mar 04 09:51:01 localhost.localdomain anacron[11023]: Job `anacron_test_id' started

The cron.daily file contains a number:

$ sudo cat /var/spool/anacron/cron.daily
20170302

Two days later the number had incremented by 2:

$ sudo cat /var/spool/anacron/cron.daily
20170304

I also tried this anacron_test.sh is in the home directory:

$ cat ~/anacron_test.sh
echo ...
(more)
edit retag flag offensive close merge delete

Comments

Are you maybe denying access by existing /etc/cron.allow and/or /etc/cron.deny?

florian gravatar imageflorian ( 2017-03-03 10:05:48 -0500 )edit

@florian, good question about path and running as root. Unfortunately it didn't make a difference. I added results to the original post under UPDATE_1.

wolfv gravatar imagewolfv ( 2017-03-03 12:07:16 -0500 )edit

Is your script set to executable with chmod +x /etc/cron.daily/anacron_test.sh? You can also check with run-parts --list /etc/cron.daily which scripts would be run.

thomaswood gravatar imagethomaswood ( 2017-03-04 03:39:55 -0500 )edit

@thomaswood the anacron_test.sh script is executable:

$ ls -l /etc/cron.daily/anacron_test.sh
-rwxr-xr-x. 1 root root 56 Mar  2 22:55 /etc/cron.daily/anacron_test.sh
$ run-parts --list /etc/cron.daily
/etc/cron.daily/anacron_test.sh
/etc/cron.daily/google-chrome
/etc/cron.daily/logrotate

and anacron_test.sh runs from the command line and from crontab.

wolfv gravatar imagewolfv ( 2017-03-04 10:01:50 -0500 )edit

@florian, It took me a while to notice, but the logs show the scheduled jobs are running. I think your suggestion of changing ~/ to absolute path fixed the scheduled jobs. Post your solution as the answer and I will mark it as the correct answer.

I was relying on anacron with option -n, but that didn't do anything:

$ anacron -n

So I should have been watching the logs more closely.

wolfv gravatar imagewolfv ( 2017-03-05 11:29:38 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2017-03-03 10:54:30 -0500

florian gravatar image

updated 2017-03-06 09:04:18 -0500

$ cat /etc/anacrontab: Maybe ~/anacron_test.sh is not the way to do it. The script is executed as root, right? So, ~ suddenly becomes /root. Try using the absolute path to the file (/home/...)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-03-03 01:34:40 -0500

Seen: 671 times

Last updated: Mar 06 '17