From 376b033c6b8fdfb8afbe42a1886b31bc7c306099 Mon Sep 17 00:00:00 2001 From: Nathan Steel Date: Sun, 29 Sep 2024 16:56:58 +0100 Subject: [PATCH] Add article for at command --- guides/at-one-off-cronjobs.html | 93 +++++++++++++++++++++++++++++++++ guides/backup-with-cron.html | 4 +- guides/index.html | 1 + 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 guides/at-one-off-cronjobs.html diff --git a/guides/at-one-off-cronjobs.html b/guides/at-one-off-cronjobs.html new file mode 100644 index 0000000..0b558e8 --- /dev/null +++ b/guides/at-one-off-cronjobs.html @@ -0,0 +1,93 @@ + + + + + + + + + + + + + at, for one-off cronjobs + + + +
+ Jump directly to main content +

at, for one-off cronjobs

+ + +
+ +
+
+ +
+
+

Scheduling a cronjob is great, but sometimes you only want it to run once. You could add, then remove the job, or write a script that executes once, but there is a better way.

+ +

Install at

+
sudo apt install at
+ +

Schedule a script with at

+

The below examples show two different ways to run a script with at.

+
echo /script/to/run | at 20:30
+
at 20:30 -f ./script/to/run
+

For this, the script needs to exist, and needs executable permissions for your user, sudo chmod +x /script/to/run, if you need a lil' hint.

+ +

The time/date passed 20:30 in this example, can then be passed in a few assorted ways.

+ +
    +
  • at now +5 minutes
  • +
  • at 11:00 today
  • +
  • at 11:00 tomorrow
  • +
  • at 20:30
  • +
  • at 20:30 093024
  • +
  • at 08:30PM Sep 30
  • +
  • at 08:30PM Sep 30 2024
  • +
  • at 08:30PM Sep 30 24
  • +
+ +

Batch a script

+

Similar to the regular means of scheduling, except the batch will wait for the system load drops to a set point.

+
echo /script/to/run | at -b 20:30
+

You can also run batch, as follows.

+
echo /script/to/run | batch 20:30
+ +

Check Queue

+
atq
+

Running the command above, will show you something akin to...

+
10      Sun Sep 29 14:14:00 2024 a user
+

Which broken down shows the ID 10, the date/time the item will run Sun Sep 29 14:14:00 2024, + the queue the item is in a, and the user executing the item user

+ + +

Remove from Queue

+

You can remove an item from the queue, with the atrm command, passing just the ID.

+
atrm 3
+ +

Mail the user

+

Like cron, at also allows the ability to mail the user about the task once executed. By default, if the command produces an output, at will mail the user, you can override this with:

+
at -M
+

This will prevent mail being sent to the user. You can also force a mail sent even if there is no output with:

+
at -m
+ +
+
+ + + + + diff --git a/guides/backup-with-cron.html b/guides/backup-with-cron.html index 4ae952c..d5d3410 100644 --- a/guides/backup-with-cron.html +++ b/guides/backup-with-cron.html @@ -47,7 +47,7 @@ rdiff-backup --force --remove-older-than 2W $DIRECTORY_TO_BACKUP_TO

Now make the script executable

chmod +x ~/scripts/backup_script.sh
-

Add a cronjob

+

Add a cronjob

Now for the automation part. Using cron we can set this script to run at many time variations. I recommend crontab guru to learn more about the expressions used for cron.

Edit the cron table (crontab)

crontab -e
@@ -104,6 +104,8 @@ fi

This script can easily be used for many different directories, on each server without needing to change the script itself. All that's needed is to change the cronjob, and/or add another cronjob changing the values it takes.

+

One off scheduling

+

If you only want something to happen once at a specific date/time, you should look into using at, for one off scheduling rather than cron.

diff --git a/guides/index.html b/guides/index.html index 5b8fc5c..7989a39 100644 --- a/guides/index.html +++ b/guides/index.html @@ -40,6 +40,7 @@

2024