By Bobby Jefferson on Monday, 21 October 2024
Category: Tech News

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.

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 (This email address is being protected from spambots. You need JavaScript enabled to view it.).
echo "lp zdnet.txt"

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:

echo "lp zdnet.txt" |

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

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.

Original link
Leave Comments