Help with cron job

I am getting a "bad day of the week" message with this cron job.

I want to run this once a day.

I rarely reboot, instead I suspend my computer most of the time.

# m h  dom mon dow   command

0 0 * * /home/andy/bin/Backup_Ubuntu_Mate.sh

crontab has 5 time parameters, you are missing one. https://crontab.guru/ allows you to play with those parms and will show you when they execute. Once a days is 0 0 * * *

But if you prefer once a day at 6:05 am, then 5 6 * * *

2 Likes

Thanks.

I will see if it works when I do not reboot and when I suspend my computer frequently.

In you script, you can add, logger message

#!/bin/sh
# commands 
# send a note to /var/log/syslog
logger Andy started backup at 6:05 am

grep Andy /var/log/syslog
(note: Andy with upper A since your pc runs under andy)

3 Likes

Thanks Mr. Pavlos for your help.

1 Like

Worth noting that if the cron job is scheduled to run at a time when the system is turned off or suspended, cron doesn't "catch up" to run missed jobs.

So, 0 0 * * * would be at midnight - but it won't run if the system was suspended at that time. :zzz: Best pick a time the computer is most likely running.

There's a utility like anacron that can help run missed jobs, but I haven't used it myself.

3 Likes

Thanks lah7.

I am studying anacron now.

As far as I know, anacron is enabled by default:
/etc/systemd/system/multi-user.target.wants/anacron.service

3 Likes

This is my anacron entry.

My script is not running.

SHELL=/bin/sh
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

# Run a backup script once a day and will work
 when computer is frequently in suspend  mode 
and may not be rebooted.
0 0 * * * /home/andy/bin/Backup_Ubuntu_Mate.sh

(Part of my script.)
Maxtor_Backup_Directory="/media/andy/5B7C-00C8/Backup/"

Icons="/usr/share/icons/Yaru/scalable/status/"
Documents="/home/andy/Documents/"
Scripts="/home/andy/bin/"
Custom_Actions_config_Directory="/home/andy/.config/Thunar/"

send a note to /var/log/syslog

logger Andy started backup at 6:05 am

cd $Documents
tar czf Documents.tar.gz *.odt *.png *.docx *.txt *.pdf
cp -a Documents.tar.gz $Maxtor_Backup_Directory
cp -a Documents.tar.gz $Western_Digital_Drive_Backup

Hey Thom, I just looked at that systemd file.

Did you know that if the computer lost power (i.e. power outage and running on internal battery), that the cron service would not run unless it was properly configured with

  • ConditionACPower=
    (empty string, i.e. false) ?

:frowning:

Yes. Anacron and cron are two different beasts which complement each other (that means you need both of them installed, which they are by default).

Cron runs, ofcourse, only when the computer has power, including batterypower. When the computer is powered down, cron does, ofcourse, not run. That means that if a job is scheduled during powerdown, it never gets executed.

This is why anacron exists. It runs the cron jobs that should have been done during powerdown. It does this after boot after a delaytime (and checks periodically after that).

So, in a normal situation, anacron has no reason to start spontaneously if the computer swiches from AC power to battery because on battery, cron just continues to run.

See man anacron

3 Likes

I have a desktop system. I am reading the anacron manual now.

This is what I am looking for.

It is Wed. 9/10/25.

I want my backup script to run once a day.

My computer is in a suspend state for about 23 hours a day.

If I am understanding correctly, if there is no reboot or shutdown, then
anacron will not run under the next reboot or shutdown occurs?

If that is the case, I will work on getting my computer to reboot once a day.

Suspending a Linux system puts it into a low-power state where everything except the memory is paused, preserving the system's working state for a quick restart. Only essential components, like memory, remain in a low-power mode, while the screen, peripherals, and CPU are turned off to save energy. Since CPU is not active, it cannot run a script for backup.

Suggestion: provide a hook in /lib/systemd/system-sleep/ to do backups before suspending. Any file in that dir is called twice, with parameter pre before suspend and with post on resume.

Sample hook:

#!/bin/sh
#

case "$1" in
    pre) 
        logger running backup before going to sleep.
        /home/andy/bin/do-backup
        logger done with backup, going to sleep.
        ;;
    post)
        logger waking up from sleep.
        ;;
esac

4 Likes

This may be the answer.

Is there something here that will recognize that the power is on, and then run my backup script after computer has resumed from a sleep state ?

https://askubuntu.com/questions/94155/run-anacron-even-when-on-battery-laptop

For Debian based systems using systemd look at /usr/share/doc/anacron/README.Debian for instructions on creating /etc/systemd/system/anacron.service.d/on-ac.conf.

Anacron for Debian
------------------
Anacron runs transparently to the system.  In other words, you should
never be aware that anacron and not cron is actually running
cron.{daily,weekly,monthly}.

Anacron itself however is not a daemon, so it will either be called at
startup, on APM power status change, on systemd timer or by cron.
Disabling those will result in some jobs not being executed on time.

Anacron leaves messages in /var/log/syslog and /var/log/messages by
default.

By default, anacron does not run while on battery power.

*Debian has added udev rules to trigger anacron when AC power is online*
*and offline, but you may still want to keep anacron running when*
*you are using battery.*

If you are using SysVinit, see /etc/default/anacron to change that.

If you are using systemd and want to run anacron even when running on battery,
you should create the following file with the specified content and then call
"systemctl daemon-reload":
    /etc/systemd/system/anacron.service.d/on-ac.conf:
        [Unit]
        ConditionACPower=

Sorry Thom, but you misunderstood my question.

That parameter setting was for anacron, which is why I asked if you were aware that there was the special attention necessary for the value assigned.

I was under the impression that the processes would run, regardless of whether the power came from AC or Battery. That is NOT the case!

If the wrong value is assigned to the parameter, cron/anacron, will not run if running on only battery power!!!

So ... did you know? because I sure didn't ... and I wonder how many others didn't either!

That is only partly true. anacron will indeed not run but cron will,
anacron is usually not supposed to run on battery.

The confusion is partly due to this unluckily formulated text:

Anacron runs transparently to the system.  In other words, you should
never be aware that anacron and not cron is actually running
cron.{daily,weekly,monthly}.

Contrary to what this text insinuates:
Anacron is not running cron at all, cron runs independently.

What they mean here is that anacron is running the daily and weekly jobs while cron is doing the hourly jobs, not that anacron is running cron.

See this part of /etc/crontab:

17 *	* * *	root	cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.daily; }
47 6	* * 7	root	test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.weekly; }
52 6	1 * *	root	test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.monthly; }

Cron will run the hourly jobs (and ofcourse all your custom jobs) and anacron will do the daily/weekly/monthly jobs.
Cron will only do the daily/weekly/monthly jobs if anacron is not installed.

Word of advice: never ever edit anacrontab, there is no scenario where that is justified. Just put your jobs in /etc/{daily,weekly,monthly}

In debian and derivatives anacron and cron both run simultaneously, each doing their own tasks:

/etc/systemd/system/multi-user.target.wants/anacron.service
/etc/systemd/system/multi-user.target.wants/cron.service

Stay with me on this:
If you are on a laptop that you want to have backed up a few times a week or you want to run updates weekly , the right thing to do is to have anacron not running on battery power.

  1. there comes a moment that your battery runs out of juice: You don't want to have a backup or update running at that time.
  2. the moment you connect it to AC, anacron will run again and start doing those jobs. You will need to connect your laptop to AC sooner or later because a charge only lasts several hours so the job will be done anyway.

This is what anacron is designed for.
Anacron is not a cron replacement and it is not designed for hourly jobs. Even the use for daily jobs is debatable.

I can not think of any scenario where it is wise to have anacron running on battery.

3 Likes

Thank you, Thom, for that excellent overview and summation of anacron. It is very much appreciated!

Since I didn't have a need for specifically scheduling jobs for my home Desktop, and that is the only exposure I've had to Linux itself (as opposed to HP-UX, Solaris, Unisys during my career), I have never really dug into the nitty gritty of the cron vs anacron.

3 Likes

Thanks.

#!/bin/sh

case "$1" in
pre)
logger Files being backed up to Maxtor drive.
/home/andy/bin/Backup_Ubuntu_Mate.sh
logger done with backup, going to sleep.
;;
post)
logger waking up from sleep.
;;
esac

1 Like