How to schedule Linux commands – and when you should


Manuel Breva Colmeiro/Getty Images

Linux has many tricks up its sleeve, which helps make it one of the most powerful and flexible operating systems on the planet. 

Take, for instance, the ability to schedule commands. Say you need to print something, but someone has been using the printer all morning and is showing no signs of letting up. You need to make sure the file in question is printed, but don’t trust your memory. This is a great time to schedule the file to print so that it’ll happen when you’re certain the printer will be available.

Also: 5 Linux commands I use to keep my device running smoothly

What do you do? Turn to at.

What is the at command?

The at command allows you to schedule the execution of a single instance of a command or script at a specific time and date and should be installed on your Linux distribution by default.

Essentially, at is run like so:

at [OPTION(s)] execution_time

Here, OPTION(s) is/are various options you can add, and execution_time is the time/date for the command’s execution.

Also: How to share folders to your network from Linux

With at, you can schedule the execution of a command at a specific time, a set number of minutes or hours after the current time, on a specific date/time, or even days into the future — it’s really flexible.

You can even schedule your computer’s shutdown at a specific time. This can be handy if you prefer to shut your PC down at night and tend to forget.

How to use at

Let’s stick with our example above, printing a file at a specific time. First, command-line printing is handled with the lp command. If you only have one printer connected to your machine, you don’t have to tell lp which printer to use. We’ll also have to use the echo command and the pipe. It sounds complicated, but it’s not. Here’s how.

The first thing you’ll need to do is open a terminal window on your Linux distribution. Once you’ve done that, verify at is installed with the command:

You should see something like this in the output:

at version 3.2.5
Please report bugs to the Debian bug tracking system (http://bugs.debian.org/)
or contact the maintainers (at@packages.debian.org).

Remember when I said the at command syntax was at [OPTION(s)] execution_time? That’s just the structure of the command itself, but doesn’t include the command you want to run with at

Also: The 4 best Linux desktops based on GNOME – and what I most like about each one

Confused? You won’t be. We have to employ the echo command to construct what we want at to run. Using our example, we’ll print the file zdnet.txt using the lp command. For that, we’ll use the following:

3. Pipe the results of the first command to at

Next, we use the pipe to send the output of the first command to the at command, which looks like this:

The output of our first command “lp zdnet.txt” just so happens to be the command to print the file zdnet.txt.

Let’s print the zdnet.txt file two hours from the current time, which would be at now + 2 hours. You could also specify a date. 

Also: Don’t run these 5 Linux commands – here’s why

Let’s say you wanted to print the file at 3pm, five days from the current date, which could be done with at 3pm + 5 days.

Our full command now looks like this:

echo "lp zdnet.txt" | at now + 2 hours

If you run that command, you’ll see output informing you when the command will be run. For instance, my output was:

warning: commands will be executed using /bin/sh
job 1 at Mon Oct 21 10:24:00 2024

Once you’ve run the command, you can be sure the zdnet.txt file will print two hours from the current time.

Also: The first 5 Linux commands every new user should learn

That’s how you use the at command in Linux. I would recommend going through the manual page (man at) to find out more about how to schedule specific times and dates with the command.





Source link

Leave a Comment