Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[HowTo] Automatically run scripts on a desktop/laptop with Anacron

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

[HowTo] Automatically run scripts on a desktop/laptop with Anacron

#1 Post by Hallvor »

Why automate commands?

Automation saves time and effort by getting repetitive tasks out of the way. This means you are getting more time for productive activities. In this HowTo, we'll use Anacron to run a script indefinitely at given intervals.


What is Anacron?

Anacron is a utility program designed to automate frequent commands. It works regardless of whether the system is running continuously or not. Unlike cron, which is another job scheduler used for executing commands at fixed intervals (perfect for servers, since they are running all the time), Anacron will execute commands in a more flexible manner. If the computer is off when Anacron is supposed to run a job, it will run the job when the computer is online again. This is perfect for laptops or desktop computers that may be offline a lot.

Setting up a custom Anacron script involves these steps:

1. Create a script (or command) that you want to run periodically
2. Determine how frequently the script should run
3. Configure Anacron
5. Verify that it works


Step 1: Create your script

First, create the script or command that you want to run periodically. This could be a Bash script, Python script, or any other executable file.

For example, let's create a simple Bash script named backupscript.sh:

Disclaimer: I have *not* tested this particular script. I only added it as an example, but it should work. Test your scripts well before using them in production environments; monitor their execution to ensure that they are running as expected.

Code: Select all

#!/bin/bash

# Source directory (replace with your actual path)
SOURCE_DIR="/path/to/source/folder"

# Destination directory (replace with your actual path, and please back up to a different disk)
DEST_DIR="/path/to/destination/folder"

# Copy command using 'cp -r' for recursive copy and '-u' to only update files that are newer than the previously copied ones
cp -ru "$SOURCE_DIR" "$DEST_DIR"

# Add a success message
echo "Folder copied successfully!"

Create the file for the script. If we were to use the script above, we could for instance save it in /usr/local/bin:

Code: Select all

# nano /usr/local/bin/backupscript.sh
Paste the content of the script into the file.

Exit with Ctrl+x, then press y to save.


Make the script executable, or it won't run

Code: Select all

# chmod +x /usr/local/bin/backupscript.sh

Step 2: Determine how frequently the script should run

Anacron allows you to specify the frequency in minutes days, weeks, or months. Here is one example from my /etc/anacrontab

Code: Select all

1       5       backup         /usr/local/bin/backupscript.sh
Let's break it down:
* The first number (1) specifies how many days there are between each time the script should be run.
* The next number (5) shows the delay in minutes for executing the script. 0 means no delay, while 30 is maximum delay. (In this case it will wait 5 minutes after rebooting or waking up the computer.)
* "backup" is just an identifier to recognize the job in logs, so you can give it any descriptive name you want.
* /usr/local/bin/backupscript.sh is the location of the script

If I want to run the script just once every 7 days (not necessarily on the same day every week) with a 5 minute delay, the syntax would be

Code: Select all

7       5       backup         /usr/local/bin/backupscript.sh
If you want to run the script once a month, you can use the monthly parameter

Code: Select all

@monthy 5       backup         /usr/local/bin/backupscript.sh 

Step 3: Configure Anacron

Open /etc/anacrontab (as root) to edit it

Code: Select all

# nano /etc/anacrontab
Enter how often the script should be run, the delay, and the path to the script, for instance:

Code: Select all

1       5       backupscript         /usr/local/bin/backupscript.sh
If you want to add logs, you can add >> /var/log/backupscript.log 2>&1

For instance:

Code: Select all

1       5       backupscript         /usr/local/bin/backupscript.sh >> /var/log/backupscript.log 2>&1
Logs will then be written to /var/log/backupscript.log

Press Ctrl+x and y to confirm to save the changes and close /etc/anacrontab


Step 6: Verify that it works

You can verify that your custom script has been added to Anacron and working as normal by checking Anacron's logs.

That's it! You can use Anacron to automate a number of jobs like system updates, disk cleanup (like deleting old log files), security scans with rkhunter, etc.


Credits:
https://www.man7.org/linux/man-pages/ma ... tab.5.html

:linked:
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

Post Reply