Shell Base 06 Control script

Source: Internet
Author: User
Tags sigint signal stdin

1. Signal Processing

Linux uses signals to communicate with processes running in the system.

Signal Value Description

1 SIGHUP Suspend process

2 SIGINT Terminate Process

3 Sigquit Stop Process

9 SIGKILL Unconditional Termination process

SIGTERM terminate the process as much as possible

SIGSTOP unconditional stop process, but not terminate process

SIGTSTP stop or pause a process without terminating the process

Sigcont continue to run the stopped process

By default, the bash shell ignores any sigquit (3) and sigterm (15) signals that are received (because of this, the interactive shell is not terminated unexpectedly). However, the bash shell handles the received Sighup (1) and SIGINT (2) signals.

If the bash shell receives a sighup signal, such as when you want to leave an interactive shell, it exits. Before exiting, however, it passes the sighup signal to all processes initiated by the shell (including the running shell script).

The shell can be interrupted by SIGINT signals. The Linux kernel stops allocating CPU processing time to the shell. When this happens, the shell transmits the SIGINT signal to all processes initiated by it to inform the state of the occurrence.

as you know, the shell will pass these signals to the shell script to handle. The default behavior of shell scripts is to ignore these signals. They may be detrimental to the script's operation. To avoid this situation, you can add the code that identifies the signal to the script and execute the command to process the signal.

(1) Signal generation

CTRL + C generates SIGINT signals and completely interrupts the process

CTRL + Z will rest their lives the SIGTSTP signal, stop the process, but without interruption, you can interrupt the process with the kill command. Use kill must have a signal mark, for example: kill-9 2456 pid 2456 process is interrupted.

(2) Capture signal

You can also not ignore the signals, capture them when they occur, and execute other commands. The trap command allows you to specify the Linux signals that the shell script will monitor and intercept from the shell. If the script receives a signal that is listed in the Trap command, the signal is not processed by the shell, but is processed locally.

Trap Commands signals

1Trap"Echo ' sorry,i have trapped Ctrl_c '"SIGINT2 Sleep 53 Echo "Hahah"4 Sleep 35 Echo "Hello"6 [[email protected] documents]$ bash test7^csorry,i have trapped Ctrl_c #在sleep时按住了ctrl +C, this sends a SIGINT signal.8 Hahah9^csorry,i have trapped Ctrl_c #在sleep时按住了ctrl +C, this sends a SIGINT signal.Ten Hello One[[Email Protected]t documents]$
View Code

(3) Remove capture

Trap--signals

1[Email protected] documents]$CatTest2#!/bin/Bash3Trap"Echo ' sorry,i have trapped Ctrl_c '"SIGINT4 Sleep 55 Echo "Hahah"6Trap--SIGINT7 Sleep 38 Echo "Hello"9 [[email protected] documents]$ bash testTen^csorry,i have trapped Ctrl_c One Hahah A^C -[Email protected] documents]$
View Code

2. Run script in future mode-----End with the end of the session

In background mode, the process runs without association with STDIN, STDOUT, and stderr on terminal sessions. Allow them to run in the background without having to occupy the terminal session, so you can do something else on the terminal.

You can run shell scripts in background mode by joining & directly.

1[Email protected] documents]$CatTest2#!/bin/Bash3 Sleep 34 Echo "Hello"5[Email protected] documents]$ Bash Test &6[1]48787[Email protected] documents]$ls8 a ltest test1 test2 test3 test8 testfile9Atest test Test1.SHTest2.SHTest3.SHTest9Ten [email protected] documents]$ Hello One^C A[1]+Done Bash Test - [email protected] documents]$ -  the  -During hibernation, you can execute other statements. When the background executes, it will automatically output to the terminal, Hello, and need to press CTRL + C to terminate.
View Code

Note1: You can start multiple background jobs at the same time at the command prompt

Note2: Once the session exits, the background process will also exit.

3. Run script under non-console-------background process does not end due to end of session

It can be implemented with the Nohup command, which runs another command to block all sighup signals sent to the process, which prevents the process from exiting while exiting the terminal session.

nohup./test1.sh &

Note1: Because the nohup command will disassociate the terminal from the process, the process will no longer be associated with stdout and stderr. To save the output produced by the command, the nohup command automatically redirects messages stdout and stderr to a file called Nohup.out.

4. Operation Control

These features of start, stop, terminate, and resume jobs are called job controls. With job control, you have complete control over how all the processes in the shell environment work.

(1) View Job

Jobs command. Allows you to view the jobs that are currently being processed.

Eg: $ jobs

[1] Running./test9.sh

[1] + Stopped./test10.sh

[2]-Running/test10.sh > Test10.out &

Note:jobs the plus and minus signs in the command output. Jobs with a plus sign are treated as default jobs. A job with a minus sign is the next default job. There is only one job with a plus sign and minus sign at any time, regardless of how many shells are running.

(2) Restart the stopped job

You can restart a job that has been stopped as a background process or foreground process.         BG Job number in the background FG operation number for front desk

5. Adjust the degree of humility

In a multitasking operating system (Linux is), the kernel is responsible for allocating CPU time to each process running on the system. Scheduling priority is the CPU time (relative to other processes) that the kernel allocates to the process. In a Linux system, the scheduling priority of all processes initiated by the shell is the same by default.

The scheduling priority is an integer value, from 20 (highest priority) to +19 (lowest priority). By default, the bash shell starts all processes at priority level.

nice-n Specific numeric Start command Eg:nice-n 4./test.sh

Note: You cannot increase the priority of a command, only reduce it.

Note2: The renice command can also lower the priority, but it can change the priority of a command that is already running, and it automatically updates the scheduling priority of the currently running process.

6. Run the job regularly

the AT command and cron tables can be used to run jobs on a timed basis.

(1) at

principle: The AT command allows you to specify when the Linux system runs the script. The AT command submits the job to the queue, specifying when the shell will run the job. The daemon for at ATD runs in background mode, checking the job queue to run the job. Most Linux distributions run this daemon at startup. The ATD Daemon examines a special directory on the system (typically located in/var/spool/at) to obtain jobs submitted by the AT command. By default, the ATD daemon checks this directory every 60 seconds. When there is a job, the ATD daemon checks when the job settings run. If the time matches the current time, the ATD Daemon will run the job. If you specify a time that is already wrong, the AT command runs the specified job at that point the next day.

Format: at [-f filename] time

By default, the AT command places the input of the stdin in the queue. You can also add the-f parameter to specify the file name used to read the command (script file). Time formats are also supported in many ways.

Eg:at-f test12.sh Now

Cons: The AT command automatically uses the SendMail application to send messages, and the content in the message is stdout and stderr content. SendMail software is also required for installation. Therefore, to avoid this, you can use output redirection in the script so that the output of the statement such as ECHO is not placed in the message by mail. You can also delete jobs that are waiting in the queue.

(2) Cron Schedule

If you want the script to run at the same time every day, or once a week, and so on, you can't use at to submit jobs continuously, and you can use a cron program to schedule jobs to be executed on a regular basis.

Format: Min hour dayofmonth month DayOfWeek command

EG:15 * Command surface will be executed at 10:15 per day of each month.

Disadvantage, the server may shut down, and those days of the shutdown command will not be executed after booting. To avoid this, you can use the Anacron program to perform programs that were previously not executed because of shutdown. ---------for operation and maintenance purposes.

Reference Documents :

Linux command line and Shell Scripting Encyclopedia (3rd edition) [Mei] Bloom (Richard Blum), Bresnahan (Christine Bresnahan), Jia, Wuhai

Shell Base 06 control script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.