I would like to set up a scheduled task via anacron but I would like to do so in user mode. How can I achieve this?
- 21,823
3 Answers
You can set up a separate anacron instance to run in user mode:
Create a
.anacronfolder in your home directory and in it two subfolders,etcandspool:mkdir -p ~/.anacron/{etc,spool}Create a new file
~/.anacron/etc/anacrontabwith contents similar to the following:# /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # period delay job-identifier command 1 10 testjob test.shAdd the following line to your crontab using
crontab -e:@hourly /usr/sbin/anacron -s -t $HOME/.anacron/etc/anacrontab -S $HOME/.anacron/spool
- 41,116
- 21,823
-
Just to confirm, this runs the specified script as the user, so a
whoamiin the script will give the user name? – Christoph Mar 02 '14 at 19:50 -
-
3I think you need to run
anacronperiodically; it seems to run once then quit. The system's anacron is called once every hour. I suppose a similar strategy would work here, i.e. call the useranacronfrom the usercrontab. – Sparhawk Nov 03 '15 at 03:00 -
1@Sparhawk agreed, in particular Ubuntu 15.10 anacron uses both
/etc/apm/event.d/anacronand/etc/init/anacron.confto take care of restarts. – Ciro Santilli OurBigBook.com Dec 09 '15 at 21:53 -
1You need to run anacron hourly with a cron job, e.g.
01 * * * * /usr/sbin/anacron -t /home/user/.anacron/etc/anacrontab -S /home/user/.anacron/spool– Simon Woodside Aug 17 '16 at 05:15 -
1This won't work if the user has an encrypted home directory. I've replaced that last
crontab -ecommand with an entry in my~/.profileinstead. – Razor Jul 24 '20 at 05:05 -
@Razor but my
.profileentry didn't get me anywhere:anacrondidn't run at login. I had tosudoeverytime to do this, requiring my password – stucash Dec 18 '21 at 09:43 -
1@Glutanimate thank you for this! It has been driving me crazy for a long time. – telometto Jan 14 '22 at 12:44
The anacrontab shown above has a problem: anacron looks for executables only in the directories specified in PATH. So it will not find test.sh.
A better solution is to use the command run-parts:
# /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
period delay job-identifier command
1 10 dailyjob run-parts ${HOME}/.anacron/daily/
This requires that we create the directory daily:
mkdir -p ~/.anacron/{etc,spool,daily}
run-parts will not execute scripts with '.' in the name, such as "test.sh", so rename the script to, for example, "my-test".
There is yet another wrinkle: If the computer does not run 24/7, then one does not have control over at what time the daily script is run. I have a web scraping script that I want to be run at about 11 am, or later during the day. I fix this problem by scheduling the web scraping job in the "at" facility (sudo apt install at).
So I have a script daily/crawl which looks as follows:
#!/bin/bash
preferred_time="11:15"
scheduled_time="${preferred_time}"
preferred_time_as_number="${preferred_time/:/}"
current_time_as_number=$(date +%H%M)
if [ "${current_time_as_number}" -ge "${preferred_time_as_number}" ]; then
scheduled_time="NOW"
fi
at -M -f "${HOME}/bin/crawl-dagpris.sh" "${scheduled_time}"
- 36
This worked for me (thanks), but I didn't use the last step given in the answer:
Then add the following line to your
~/.profile:
I'm using Ubuntu Studio 12.10 Quantal and in my case instead of that last step I put that one liner here: “Applications Menu” → “Settings” → “Settings Manager” then in the Settings Manager under “Session and Startup” then the “Application Autostart” tab.
This is for those of us that are GUI users, because ~/.profile is only sourced by bash when it starts a log-in interactive shell (even ~/bashrc is not so useful since that is only sourced when bash is starting an interactive shell).
- 8,496
- 1
-
Mmh, I am a GUI user myself and the line in
.profilehas always worked fine for me (Ubuntu 12.04 here). – Glutanimate Sep 12 '13 at 14:33
sudo -u <user>before the task inanacrontab– Karthik T May 20 '14 at 14:34