Shell Script—Threading

This article mainly introduces threads in the shell, waits and signals in threads;

1. thread

There are many ways to implement threads in the Shell, and currently only the pass symbol is introduced &; by adding &a symbol at the end of the command, a process can be started in the background and return immediately, allowing the Shell process to continue to execute other commands;

example

#!/bin/bash

NUM=20

function fun1()
{
    
    
	for ((i=0;i<${NUM};i++));do
		echo -n "fun1"   #-n不换行输出
	done
}

function fun2()
{
    
    
	for ((i=0;i<${NUM};i++));do
		echo -n "fun2"
	done
}

function fun3()
{
    
    
	for ((i=0;i<${NUM};i++));do
		echo -n "fun3"
	done
}

function fun4()
{
    
    
	for ((i=0;i<${NUM};i++));do
		echo -n "fun4"
	done
}

#换行
fun1 &
fun2 &
fun3 &
fun4 &

#不换行也可以
#fun1 & fun2 & fun3 & fun4

#sleep 0.001   #等待0.001秒
sleep 1   #等待1秒
echo -e "\n"

output

fun1fun1fun1fun1fun1fun1fun1fun1fun1fun1fun1fun1fun2fun1fun1fun2fun1fun2fun1fun1fun2fun1fun2fun1fun2fun1fun2fun2fun2fun2fun2fun2fun2fun2fun2fun2fun2fun2fun2fun2fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun4fun3fun4fun4fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3fun3

In the example, 4 functions are defined, fun1, fun2, fun3 and fun4, and each function is printed in a loop without wrapping;

2. wait

In the shell, you can use different methods to implement the wait operation, depending on the task or condition you want to perform during the wait.

2.1 sleep command

sleepCommands: sleepCommands can be used to pause the execution of a shell script for a specified period of time. It uses the following syntax:

sleep <duration>

Among them <duration>, can be a specific time (in seconds), and you can directly specify the number of seconds to pause, for example, sleep 5means to pause execution for 5 seconds;

In addition to seconds, sleepthe command also supports the use of units after the number to specify other time units. Commonly used time units include:

  • s: seconds (default unit)
  • m:minute
  • h:Hour
  • d:sky

You can specify different units as needed to achieve more precise time intervals. Here is an example:

sleep 0.01    # 暂停执行 0.01 秒

sleep 30s    # 暂停执行 30 秒

sleep 2m     # 暂停执行 2 分钟

sleep 1h     # 暂停执行 1 小时

sleep 3d     # 暂停执行 3 天

This way, you can specify sleepthe time interval of the command according to your needs, using the appropriate units.

2.2 Loop waiting

A loop can be used to keep checking a condition until the condition is met. For example:

while [ ! -f /path/to/file ]
do
    sleep 1
done

2.3 Signal Processing

Commands can be used trapto specify that certain commands be executed when a specified signal is received. You can use signals to trigger waiting operations;

example

#!/bin/bash

#通过信号来进行线程的等待
trap "continue_script=1" SIGUSR1  # 定义一个自定义信号的处理函数
continue_script=0  # 用于控制脚本是否继续执行

echo "begin"
# 在需要等待的地方检查条件
while [ $continue_script -eq 0 ]
do
        echo "$continue_script"
        sleep 1
done

echo "end"

Execution kill -SIGUSR1 2059, where 2059is the process number, you can ps -ef | grep *.sh | grep -v grepcheck the process number through ;

output

begin
0
0
0
0
0
0
end

In the example, a while loop is defined, and the loop condition is $continue_script -eq 0, when SIGUSR1the signal is sent to, $continue_script the value becomes 1, and the loop condition is terminated;

3. Signal

In the shell, there are many different signals, some common ones and what they mean are listed below:

  1. SIGINT(2): The interrupt signal sent by the terminal, such as when using Ctrl+C.
  2. SIGQUIT(3): The exit signal sent by the terminal, for example when using Ctrl+\.
  3. SIGKILL(9): A forced termination signal that cannot be caught or ignored.
  4. SIGTERM(15): The default termination signal, used to terminate the process normally.
  5. SIGHUP(1): The signal that the terminal connection is disconnected is usually used to notify the process to reload the configuration file.
  6. SIGUSR1(10) and SIGUSR2(12): User-defined signals, which can be used for custom operations.
  7. SIGSTOP(19) and SIGTSTP(20): Signals used to stop a process, for example when using Ctrl+Z.
  8. SIGCONT(18): The signal to continue executing the stopped process.
  9. SIGPIPE(13): A signal triggered when writing to a closed pipe.
  10. SIGCHLD(17): The signal sent to the parent process when the child process ends or stops.

These are just some common signals, different operating systems and shells may have others. You can use kill -lthe command to view the list of signals available on the system and their corresponding numbers;

Use trapcommands to register signal handlers and customize actions to be performed when specific signals are received;

exampleSIGINT

#!/bin/bash

cond=0
function fun()
{
    
    
	echo "echo get sigint, will finished!"
	cond=1
}

trap fun SIGINT

while [ ${cond} -eq 0 ];do
	sleep 1
done

After executing the script, ctrl+c will trigger

output

echo get sigint, will finished!

Guess you like

Origin blog.csdn.net/shouhu010/article/details/131411686