Bash scripts: start
Bash scripts, part 2: loops
Bash scripts, part 3: command line options and keys
Bash scripts, part 4: input and output
Bash Scripts, Part 5: Signals, Background Tasks, Script Management
Bash scripts, part 6: functions and library development
Bash scripts, part 7: sed and word processing
Bash scripts, part 8: awk data processing language
Bash scripts, part 9: regular expressions
Bash scripts, part 10: practical examples
Bash scripts, part 11: expect and automate interactive utilities


| Signal code | Title | Description |
| one | SIGHUP | Terminal closing |
| 2 | SIGINT | Process stop signal by the user from the terminal (CTRL + C) |
| 3 | SIGQUIT | Process stop signal by the user from the terminal (CTRL + \) with a memory dump |
| 9 | Sigkill | The final completion of the process |
| 15 | Sigterm | Process completion request signal |
| 17 | Sigstop | Forced suspension of the process, but not the completion of its work |
| 18 | Sigtstp | Suspending the terminal process (CTRL + Z), but not shutting down |
| nineteen | Sigcont | Continuation of the previously stopped process |
SIGHUP signal when you close the terminal, it terminates. Before exiting, it sends a SIGHUP signal to all processes running in it, including running scripts.SIGINT signal temporarily stops the operation. The Linux kernel no longer allocates processor time to the shell. When this happens, the shell notifies the processes by sending them a SIGINT signal.CTRL + C key combination generates a SIGINT signal and sends it to all processes running in the shell, which leads to their completion. $ sleep 100 CTRL + C key combination.
CTRL + Z key combination allows you to generate a SIGTSTP signal, which pauses the process but does not complete its execution. Such a process remains in memory, its work can be resumed. Run the command in the shell: $ sleep 100 CTRL + Z
ps –l 
T is displayed in column S , which displays the status of the process. This indicates that the command is either suspended or in the trace state.kill command. Details about it can be read here . kill processID trap command. If the script receives the signal specified when calling this command, it processes it independently, and the shell will not process such a signal.trap command allows the script to respond to signals, otherwise they are processed by the shell without its participation.trap command, you trap code to be executed and the list of signals separated by spaces that we want to intercept. In this case, this is just one signal: #!/bin/bash trap "echo ' Trapped Ctrl-C'" SIGINT echo This is a test script count=1 while [ $count -le 10 ] do echo "Loop #$count" sleep 1 count=$(( $count + 1 )) done trap command used in this example displays a text message whenever it detects a SIGINT signal, which can be generated by pressing Ctrl + C on the keyboard.
CTRL + C , the script executes the echo command specified by the trace call instead of letting the shell exit.EXIT when calling the trap command: #!/bin/bash trap "echo Goodbye..." EXIT count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$(( $count + 1 )) done 
SIGINT signal, the interception will trigger and the shell will execute the echo command.trap command with new parameters: #!/bin/bash trap "echo 'Ctrl-C is trapped.'" SIGINT count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$(( $count + 1 )) done trap "echo ' I modified the trap!'" SIGINT count=1 while [ $count -le 5 ] do echo "Second Loop #$count" sleep 1 count=$(( $count + 1 )) done 
trap command, passing it a double dash and the name of the signal: #!/bin/bash trap "echo 'Ctrl-C is trapped.'" SIGINT count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$(( $count + 1 )) done trap -- SIGINT echo "I just removed the trap" count=1 while [ $count -le 5 ] do echo "Second Loop #$count" sleep 1 count=$(( $count + 1 )) done trap command. Run the script: $ ./myscript CTRL + C on the keyboard.
CTRL + C was at the time the script was executed, when the signal interception was in effect, so the script executed the echo command assigned to the signal. After the execution reached the intercept cancellation command, the CTRL + C command worked in the usual way, exiting the script.ps command, you might have noticed processes that run in the background and are not tied to the terminal. #!/bin/bash count=1 while [ $count -le 10 ] do sleep 1 count=$(( $count + 1 )) done & ) after the name: $ ./myscipt & 
STDOUT and STDERR , that is, the text or error messages it displays can be seen in the terminal.
nohup command to do this. This command allows you to start the program, blocking the SIGHUP signals sent to the process. As a result, the process will be executed even when exiting the terminal in which it was launched. nohup ./myscript & 
nohup command unbinds the process from the terminal. This means that the process will lose references to STDOUT and STDERR . In order not to lose the data output by the script, nohup automatically forwards messages arriving in STDOUT and STDERR to the file nohup.out .nohup.out .jobs command allows you to view current jobs that are running in the shell. Let's write this script: #!/bin/bash count=1 while [ $count -le 10 ] do echo "Loop #$count" sleep 10 count=$(( $count + 1 )) done $ ./myscript CTRL + Z
$ ./myscript > outfile & jobs command, we will see information about both the suspended script and the one that works in the background.
-l switch when invoking the jobs command indicates that we need information about process ID .bg command. $ ./myscript CTRL + Z , which temporarily stops its execution. Run the following command: $ bg 
bg command to restart a specific task.fg command: $ fg 1 at command and the cron job scheduler. at [-f filename] time now , noon , midnight .at command can also be passed a date using one of its supported formats.MMDDYY , MM/DD/YY , or DD.MM.YYJul 4 or Dec 25 , while the year can be specified, or you can do without it.now + 25 minutes .10:15PM tomorrow .10:15 + 7 days . $ at -f ./myscript now 
-M key when calling at used to send what the script displays to the email if the system is properly configured. If sending the email is not possible, this key will simply suppress the output.atq command: $ atq 
atrm command atrm . When you call it indicate the job number: $ atrm 18 
at command can make life easier in many situations. But what if you want the script to run at the same time every day, or once a week, or once a month?crontab utility that allows you to schedule scripts to run regularly.Crontab runs in the background and, based on data in the so-called cron-tables, runs jobs on a schedule.cron job table, use the following command: $ crontab –l crontab accepts data on when the task should be performed in the following format: , , , , . command executed daily at 10:30, this would correspond to the following entry in the task table: 30 10 * * * command * ", used for the fields that specify the day of the month, the month and the day of the week, indicates that cron must execute the command every day of every month at 10:30.4:30PM every Monday, you will need to create the following entry in the task table: 30 16 * * 1 command 00 12 1 * * command crontab with the -e key: crontab –e 30 10 * * * /home/likegeeks/Desktop/myscript $ rm -f /var/run/crond.pid cron , using several special directories: /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly $HOME/.bash_profile $HOME/.bash_login $HOME/.profile .bash_profile file..bashrc .
Source: https://habr.com/ru/post/326826/
All Articles