linux bash scripting

Linux Bash Scripting Part5 – Signals and Jobs

In the previous post, we talked about input, output, and redirection in bash scripts. Today we will learn how to run and control them on Linux system. Till now, we can run scripts only from the command line interface. This isn’t the only way to run Linux bash scripts.

This post describes the different ways to control your Linux bash scripts.

Linux signals

These are the most common Linux system signals:

Num        Name                    Job

1              SIGHUP               Process hangs up.

2             SIGINT                 Process Interruption.

3             SIGQUIT              Proces quit or stop.

9             SIGKILL               Process termination.

15           SIGTERM             Process termination.

17           SIGSTOP              Process stopping without termination.

18           SIGTSTP              Process stopping or pausing without termination.

19           SIGCONT             Process continuation after stopping.

Your Linux bash scripts don’t control these signals, you can program your bash script to recognize signals and perform commands based on the signal that was sent.


Stop a process

To stop a running process, you can press Ctrl+C which generates SIGINT signal to stop the current process running in the shell.

$ sleep 100


stop process

Pause a process

The Ctrl+Z keys generate a SIGTSTP signal to stop any processes running in the shell, and that leaves the program in memory.

$ sleep 100


pause process

The number beside the plus sign which is 1 is the job number assigned by the shell.

If try to exit the shell and you have a stopped job assigned to your shell, the bash warns you if you.

The ps command is used to view the stopped jobs.

ps l

ps -l

In the S column (process state), it shows the stopped job’s state as T. This indicates that the command is either being traced or stopped.

If you want to terminate a stopped job you can kill its process by using kill command I recommend you to review the basic Linux commands if you need more info about kill command.

kill processID

Trapping signals

The trap command allows you to specify which Linux signals your shell script can watch from the shell. If the script gets a signal defined by the trap command, it stops processing and instead the script handles the signal.

You can trap signals using the trap command like this:

The trap command in this example shows a simple text message each time it detects the SIGINT signal when hitting Ctrl+C.

trap signal

If you press Ctrl+C, the echo statement specified in the trap command is printed instead of stopping the script. Cool, right?

Trapping the script exit

You can trap the shell script exit, just add the EXIT signal to the trap command:

trap exit

When the bash script exits, the Goodbye message is printed as expected.

Also, if you exit the script before finishing its work, the EXIT trap will work.

Modifying or removing a trap

You can reissue the trap command with new options like this:

modify trap

After the signal trap is modified, the bash script manages the signal or signals differently.

You can also remove a trap. Simply add two dashes after the trap command.

If the script receives a signal before the trap is removed, the script processes it using the original trap command.

$ ./myscript


remove trap

The first Ctrl+C was used to terminate the script. Because the signal was received before the trap was removed, the script executed the echo command specified in the trap. After the trap is removed, you can exit the script by pressing Ctrl+C as normal.

Running Linux bash scripts in background mode

Sometimes your Linux bash scripts can take a long time to process, and you have to wait until the process finishes. Luckily, there’s an easy solution for this problem.

If you see the output of the ps command, you will see all the running processes in the background and not tied to the terminal.

We can do the same, just place ampersand symbol after the command.

$ ./myscipt &

run in background

Once you’ve done that, the script runs in a separate background process on the system and you can see the process id between the square brackets and when the background process finishes, it displays a message on the terminal that it is done.

Notice that while the background process is running, you can use your terminal monitor for STDOUT and STDERR messages so if an error occurs, you will see the error message and normal output.

run script in background

The background process will exit if you exit your terminal session.

So what if you want to continue running even if you close the terminal?

Running Scripts without a Hang-Up

You can run your Linux bash scripts in the background process even if you exit the terminal session.

You can do this using the nohup command.

The nohup command blocks any SIGHUP signals. This blocks the process from exiting when you exit your terminal.

$ nohup ./myscript &

linux bash nohup command

The nohup command makes your process loses the STDOUT and STDERR output links. To get any output generated by the command, the nohup command redirects STDOUT and STDERR messages to a file called nohup.out.

Note: when running multiple commands from the same directory will override the nohup.out file content.

Viewing jobs

The jobs command enables you to view the current jobs being handled by the shell.

Then run it.

$ ./myscript

Then press Ctrl+Z to stop the script.

linux bash view jobs

Run the same bash script but in the background using the ampersand symbol and to make it easier, I’m going to make the output of that script is redirected to a file so it doesn’t appear on the screen.

$ ./myscript > outfile &

linux bash list jobs

The jobs command shows the stopped and the running jobs.

jobs l

-l parameter to view the process ID

Restarting stopped jobs

The bg command is used to restart a job in background mode.

$ ./myscript

Then press Ctrl+Z

Now it is stopped.

$ bg

linux bash restart job

After using bg command, it is now running in background mode.

If you have multiple stopped jobs, you can do the same by specifying the job number to the bg command.

If you want to restart a job in foreground mode, use the fg command.

$ fg 1

Scheduling a job

The Linux system provides 2 ways to run a bash script at a predefined time: the at command and the cron table.

The at command

This is the format of the command

at [-f filename] time

The at command can accept different time formats:

  • A standard hour and minute, such as 10:15.
  • An AM/PM indicator, such as 11:15PM.
  • A specifically named time like now, midnight.

You can include a specific date, using some different date formats:

  • A standard date format, such as MMDDYY or DD.MM.YY.
  • A text date, such as June 10 or Feb 12, with or without the year.
  • Now + 25 minutes.
  • 05:15AM tomorrow.
  • 11:15 + 7 days.

We don’t want to dig deep into the at command, but for now, just make it simple.

$ at -f ./myscript now

linux bash at command

The -M parameter is used to send the output to email if the system has email, and if not, this will suppress the output of the at command.

To list the pending jobs, use atq command:

linux bash at queue

Remove Pending Jobs

To remove a pending job, use the atrm command:

$ atrm 18

delete at queue

You must specify the job number to the atrm command.

Scheduling scripts

You can use the at command to schedule a script to run at a specific time, but what if you need that script to run at the same time every day or once a week or once a month.

The Linux system uses the crontab command to allow you to schedule jobs that need to run regularly.

To list an existing cron table, use the -l parameter:

$ crontab l

The format for crontab is:

minute,Hour, dayofmonth, month, and dayofweek

So if you want to run a command daily at 10:30, type the following:

30 10 * * * command

The wildcard character (*) used to indicate that the cron will execute the command daily on every month at 10:30.

To specify a command to run at 4:30 PM every Tuesday, you would use the following:

30 16 * * 2 command

The day of the week starts from 0 to 6 where 0 is Sunday and 6 is Saturday.

To run a command at 12:00 on the first day of every month:

00 12 1 * * command

The day of the month is from 1 to 31.

Let’s keep it simple for now and we will discuss the cron in great detail in future posts.

To add entries to your cron table, use the -e parameter like this:

crontab e

Then type your command like the following:

 30 10 * * * /home/likegeeks/Desktop/myscript

This will schedule our script to run at 10:30 every day.

Note: sometimes you see error says Resource temporarily unavailable.

All you have to do is this:

$ rm -f /var/run/

You should be a root user to do this.

Just that simple!

You can use one of the pre-configured cron script directories like:





Just put your bash script file on any of these directories and it will run periodically.

Starting scripts with a new shell

In the previous posts, we’ve talked about startup files, I recommend you to review the previous.




Just place any scripts you want to run at login time in the first file listed $HOME/.bash_profile.

OK, what about running our bash script when the shell opens? Easy.

Type your script on .bashrc file.

And now if you open the shell window, it will execute that command.

I hope you find the post useful. keep coming back.

Thank you.