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
Martin Schröder
  • 6,772
  • 1
  • 19
  • 35
Philippe Gachoud
  • 5,800
  • 3
  • 41
  • 50
  • Let's start with this: run 'sudo crontab -u [username] -l' and tell me if the cron job is there, please. – RGS Jun 02 '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 02 '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 03 '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 03 '13 at 13:30
  • @Alaa reason is if my computer isn't tuned on.... its a workstation, not a server ;-) – Philippe Gachoud Jun 04 '13 at 16:44
  • `cron` and `anacron` will never turn on the computer to run cronjobs. From my search, what you're looking for is to combine the tool [`rtcwake`](http://linux.die.net/man/8/rtcwake) and cron. `rtcwake` can be used to suspend your machine and to specify a time for it to wake up automatically. You can read [this](http://www.osnews.com/story/24111/), [this](http://hajimu.org/?p=2936), and [this](http://www.howtogeek.com/121241/how-to-make-your-linux-pc-wake-from-sleep-automatically/). – Alaa Ali Jun 06 '13 at 06:22
  • 1
    Possible duplicate of [How can I run anacron in user mode?](http://askubuntu.com/questions/235089/how-can-i-run-anacron-in-user-mode) – Sparhawk Nov 03 '15 at 02:31

1 Answers1

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.

ge32
  • 319
  • 2
  • 5