Link to home
Create AccountLog in
Avatar of David197705

asked on

Need some assistance with anacron in Ubuntu


I've got Ubuntu 14.04 I try to learn about using anacron. And there're 3 things so far that I can't understand. Please explain!

1.) WHEN does the anacron command (tast) should be executed for the FIRST TIME?
Let's say today is Wednesday 2 p.m. (14:00) and I've just made a new weekly task for anacron. So when should I be expecting the first execution of it? In other words, what's the starting point here? Weekly, starting from...???

2.) Let's say I've got 3 commands. So this MUST be a script OR... I can stuff all three commands in ONE single anacron task? To be more specific, I want to stop the process, delete the file's content and then to re-start the same stopped process.

3.) Where should I put the commands for anacron? They say that in /etc/anacrontab, but there I see 3 default lines of commands that govern the cron (if I understand that correctly). Should I write my own commands in the same place just below those three?
Avatar of Jan Bacher
Jan Bacher
Flag of United States of America image

If it were me, I'd create a bash script with all three commands and put it in my home bin directory, i.e.,


chmod 755 /home/jesper/bin/myscript

Then create a regular cron job in cron.d.  Restart crond to pick up the new job.
anacrontab just kicks off the cron* scripts, check the man page for anacrontab

If you want to schedule a job make sure your script is executable and just add it to either /etc/crontab
the following example with run /path/to/command every hour as root.

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
0 *  *  *  * root /path/to/command

Open in new window

or you can create it as your own by running crontab -e and you would just need

0 *  *  *  * /path/to/command

Open in new window

If you have something more specific you want to ask post the details here.
Avatar of David197705


Thank you both for your replies!
I'll tell you what I did exactly to be on the same wave. I created a bash script AS ROOT (with gksudo nautilus command). Then I made it executable. There're two ways -- one is in a graphic Windows-like way (by checking the option of turning the file into executable) and the other way is with this command:
chmod +x . But then I tried both ways and in ls -la I saw exactly the same attributes of the script. After that I tried to run the script from the command like. It only ran with sudo. The name I gave to the script was like that (hope it's OK). Right now the script's attributes I get with ls -la command are:
Now... I have a few things to ask:
1.) In the script I wrote my commands without sudo. Like I said above, I had to use sudo to be able to run it in the terminal. So what about cron/anacron? Does it act as root, so it has enough privileges to run the script the way it is (as I described)?
2.) In /etc/passwd root has /bin/bash, but in both /etc/anacrontab and /etc/crontab it's written:
SHELL=/bin/sh. Is it Okay?
3.) Do I have to chmod my script (as Jan Springer suggested) or it's fine as it is (cron/anacron-wise)?
4.) The location of the script... Does it matter? Jan Springer suggested putting it in " /home/jesper/bin/myscript" location, and right now on my computer the script resides in /home/myname/Downloads directory (I usually keep my stuff in it, so I know where to find it).

All right, please do answer my questions and then we'll take it from there.
Example filename:


In the file:

0 3 * * * root /home/jesper/bin/myscript > /dev/null 2>&1

Runs the script at 03:00 every day.  Run it twice a day:

0 3,15 * * * root /home/jesper/bin/myscript > /dev/null 2>&1

And restart crond.

You don't have to be sudo to write the script.  You shouldn't even have to be sudo to chmod the script.  But you will have to sudo to create the cron job and restart crond.
Thanks for your reply!

I understand what you said about sudo. But since haven't corrected me, I assume that what I did was right (root being the owner of the script and not having sudo inside of it). I also assume that my script has the right attributes (I chmoded it OK).
I would put the file where you proposed. I googled that it's still the best place for it.
As per cron... My laptop is off most of the time, that's why I want to use anacron. So the cron job you proposed wouldn't get done 9 times out of 10, unfortunately. Regardless of that... in the cron/anacron command to execute the script... From what you'd written I understood that I should put root in it, right?
To use anacron, add your entry to /etc/anacrontab.

7       15      test.daily      /bin/bash /home/jesper_bin/myscript

It specifies to run every 7 days, check 15 minutes after the device is available, test daily, use bash as your interpreter, and the script.
Thanks for your reply!

But if you create this way a command to run every 7 days, then why test.daily? Or it's just a name? And if so, then why you chose it? It confuses me.
lack of sleep:  cron.daily

this looks for tasks daily that need to be run.
Wait a minute, but you wrote that I should put this command into /etc/anacrontab. And now you're talking about cron.daily? Shouldn't there be a name that we choose after the delay in minutes?
Like this:
 7       15      anynamewelike     /bin/bash /home/jesper/bin/myscript

P.S. I created a folder named "bin" in my /home/myusername directory. Then I put my script in it.  Then I tried to add its location into PATH like so:
export PATH=$PATH:/home/myusername/bin

Open in new window

Don't know if it's needed for anacron to do its job though. Anyhow, it doesn't work, unless I cd to the directory in which the script resides and do in terminal "sudo ./", I keep getting "command not found" error.
Are you providing full paths to the script and the commands within the script?

And, yes, "cron.daily" says to run this daily but when placed in the anacron configuration it says if the device is available.

I use both crond and crontab.  Reading the information for anacron, this makes perfect sense to me.
Okay, to put it all together... If I want to use anacron, then I have to add my entry to /etc/anacrontab.

7       15      cron.daily      /bin/bash /home/myname/bin/myscript

That's all or anything I've missed?
Avatar of Jan Bacher
Jan Bacher
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks a lot! Okay, I'll grant you your points and follow your advice. Hopefully everything would work as expected.
If not holler either here or via pm.  All of my machines are *nix (ubuntu, centos, mac) and I can test locally.