Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 2509

Docs, HowTos, Tips & Tricks • [HowTo] Automatically run scripts on a desktop/laptop with Anacron

$
0
0
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:

#!/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 onescp -ru "$SOURCE_DIR" "$DEST_DIR"# Add a success messageecho "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:

# 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:

# 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:

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:

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:

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

Step 3: Configure Anacron

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

Code:

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

Code:

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:

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 the Anacron 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

Statistics: Posted by Hallvor — 2024-03-25 17:25 — Replies 0 — Views 18



Viewing all articles
Browse latest Browse all 2509

Trending Articles