Hidden Gems: Powerful Unix Commands to Boost Your Tech Skills
Essential Bash Commands with Easy-to-Follow Examples
Throughout our software engineering careers, we've experienced the incredible power of bash commands. In this brief tutorial, I'll introduce you to some practical commands that could prove quite useful.
The disown Command
Before jumping into the disown
command, let’s understand how it relates to nohup
.
I suppose that most of you are already familiar with the nohup
command. For those who aren’t, it stands for “no hangup.” It prevents the running processes from receiving the SIGHUP
signal. This way, your script will still be running even if you close the terminal session.
A common usage of nohup
: Imagine that you need to execute a long-running script on a remote server. If you lose the connection to the server, your script will stop. This can be very frustrating! To avoid this potential issue, you can take advantage of the nohup
command.
For example:
$ nohup ./your-script.sh &
Note that nohup
doesn’t put the process in the background. You can provide the ampersand (&)
command at the end to achieve this result.
How is disown
related to nohup
?
Suppose you have already kicked off your script on the remote server without nohup
. Then you realize that it would have been wiser to run it with nohup
because the execution will take a long time.
Here is when the disown
command comes to the rescue. It is a built-in bash command that helps you put an already-running process under nohup
. You can remove jobs from the table of active jobs or mark jobs to protect them from the SIGHUP
signal.
It accepts three parameters:
-a
— Deletes all jobs if no job ID is passed.-h
— Each job ID is marked so thatSIGHUP
is not sent to the job if the shell receives aSIGHUP
signal.-r
— Deletes jobs in “running” status.
Usage demo
To demonstrate the command’s usages, create a test file called show-time.sh
and paste the following content:
#!/bin/bash
while true; do
sleep 5
date +"Hi, the current time is %F %T"
done
Make the script executable by running:
$ chmod +x show-time.sh
Start it to see the output:
$ ./show-time.sh
After an initial delay of five seconds, you’ll see the current date and time every five seconds.
Let’s see how to disown the process to make it run under nohup
:
Pause the running script by pressing
Ctrl + Z
on your keyboard:
$ ./show-time.sh
Hi, the current time is 2020–11–30 22:07:03
[1]+ Stopped ./show-time.sh
Type
bg
, a job control command, to bring the process to the background:
$ bg
List the active jobs, including the process ID, using the
jobs
command:
$ jobs -l
[1]+ 15791 Stopped ./show-time.sh
Execute
disown -h {jobid}
ordisown -h %1
if that’s the first job:
$ disown -h 15791
You can verify that the job is no longer in the jobs table:
$ jobs -l
The command won’t give you any results.
Close the terminal window and open a new one. Check if the job is still running:
$ ps -e | grep 'show'
15791 ? 00:00:00 show-time.sh
As you can see, the process is still alive even though we killed the terminal!
The at Command
Sometimes, we want to run a job on a regular schedule. We would use crontab
for that purpose. But what if we need to execute the job only once at a specified time? The at
command provides a simple solution.
If it hasn’t been installed on your system, you can install it from the terminal.
For Ubuntu/Debian distributions:
sudo apt install at
For CentOS/Fedora distributions:
sudo yum install at
Usage demo
Let’s schedule our
show-time.sh
script for two minutes from now:
$ at now + 2 minutes -f show-time.sh
job 10 at Mon Nov 30 22:44:00 2020
Note that you could also use the pipe
command to achieve the same result:
$ ./show-time.sh | at now + 2 minutes
You can also schedule a command instead of a script:
$ echo “hello world” > at.txt | at now + 3 minutes
After three minutes, this command should create a new file called at.txt
and write “hello world” into the file.
Check the output:
$ cat at.txt
hello world
The batch Command
The batch
command is useful if you want to schedule and run a job when the system load is low enough (usually below 1.5). You can modify the default load value by invoking the atd
daemon.
Usage demo
Let’s see how batch
works with another example.
Execute the
top
command to check the system load:
$ batch
top — 23:11:26 up 4:22, 1 user, load average: 0,31, 0,42, 0,46
I suggest using a separate terminal tab for the batch
command. This way, you can observe the system load and make experiments.
Execute the
batch
command. You’ll be prompted to provide actions. You can specify multiple commands here. For simplicity, let’s print “hello world” into a new text file calledhello.txt
and then create an emptytst.txt
file:
$ batch
at> echo "hello world" > hello.txt
at> touch tst.txt
at> <EOT>
job 14 at Mon Nov 30 23:07:00 2020
Note that you have to press Ctrl + D
when you are ready to exit the command prompt.
Let’s check if the files have been created:
$ ls
hello.txt tst.txt
The system load was low enough, so the files have been created without any issues.
Summary
We’ve seen why the disown
, at
, and batch
commands are handy and how to use them.
Key takeaways:
disown
removes jobs from the job table so your process is alive even when the current shell terminates.at
schedules jobs meant to be run only once at a particular time.batch
executes jobs when the system resources are low enough.
I recommend exploring the documentation to find out more about these commands’ features.
I hope that you’ve enjoyed this tutorial and can apply your new knowledge to practice. Thank you for reading and happy coding!