4

I want a special command to be lauched everyday, but dont want to put it in the /etc/cron.d/ because its a user specific user!

pg@pipoTower: ~$ crontab -l
# m h  dom mon dow   command
0 0 * * * updatedb -l 0 -o ~/.externalharddisk.db -U /var/autofs/removable/usbData

The crontab -e command which puts the file in /var/spool/cron/crontabs/userName

but dont know why its not called by anacron....

usr@Tower: ~/$ cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/root
LOGNAME=root

# These replace cron's entries
1       5       cron.daily      run-parts --report /etc/cron.daily
7       10      cron.weekly     run-parts --report /etc/cron.weekly
@monthly        15      cron.monthly    run-parts --report /etc/cron.monthly
7
  • Let's start with this: run 'sudo crontab -u [username] -l' and tell me if the cron job is there, please. – RGS Jun 2 '13 at 12:18
  • 1
    anacrontab is not the program that calls the files under /var/spool/cron/crontabs/, cron is. anacrontab is the program that calls whatever is in cron.daily, .weekly, and .monthly. – Alaa Ali Jun 2 '13 at 13:01
  • @Alaa Thx for your confirmation, but doesn't help me... how do I do it for /var/spool/cron/crontabs/ to be called by anacron? – Philippe Gachoud Jun 3 '13 at 13:08
  • Is there a reason you want anacron to call the files in that location? Because cron automatically calls those files. In your question, you have already created a crontab for the user pg to run that updatedb command every day at midnight, right? Then that's it, your cronjob is functional now. If you want to check if it's working, do cat /var/log/syslog | grep CRON | grep updatedb, you should see a line(s) like CRON[###]: (pg) CMD (updatedb -l 0 -o ~/.externalharddisk.db -U /var/autofs/removable/usbData). If you do, then your cronjob is working. – Alaa Ali Jun 3 '13 at 13:30
  • 1
    Possible duplicate of How can I run anacron in user mode? – Sparhawk Nov 3 '15 at 2:31
11

I've found two workarounds for this...

Option 1: Have cron.daily script run as non-root user

Scripts in /etc/cron.daily will run as root, but root doesn't need a password to run as someone else. So put a script there that does something like this:

su myuser -c "/home/myuser/dostuff.sh"

The main drawback if you need to be root to set this up.

Option 2: Set up a personal anacron

Create your own anacron script directories, eg ~/.anacron/daily, ~/.anacron/weekly and ~/.anacron/monthly. Put your daily scripts in the daily directory.

Then create your own anacrontab file, eg ~/.anacron/anacrontab. And make it say this:

1   5   cron.daily  run-parts --report /home/myuser/.anacron/daily
7   10  cron.weekly run-parts --report /home/myuser/.anacron/weekly
@monthly    15  cron.monthly    run-parts --report /home/myuser/.anacron/monthly

Also make a spool directory for your anacron task, eg ~/.anacron/spool.

Finally you need make anacron run. Use a frequent cron job for this. Run crontab -e and add

* * * * * /usr/sbin/anacron -t /home/myuser/.anacron/anacrontab -S /home/myuser/.anacron/spool

This will run anacron with your config every minute and if a job is due anacron will run it. You can make it less frequent if you like, every hour might be good enough for most users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.