Get the App
SLTechnology News&Howtos  ›  Database  › 

Old Boys Education Daily question: March 9, 2017-Please explain the meaning and application scenario of the whole line of code at the beginning of if in the following Shell script.

Shulou Source: shulou.com Published: 2022-06-01 21:13:57 10月04日 Update

Please explain the meaning of the whole line of code that begins with if in the following Shell script. Have you seen its application scenario?

If (kill-0$ pid 2 > / dev/du11) then echo "oldboy" else echo "oldgirl" fi

Interview question: please explain the meaning of if (kill-0$ pid 2 > / dev/null) code?

If (kill-0$ pid 2 > / dev/null) then echo "oldboy" else echo "oldgirl" fi

The old boy answered the main points:

The-0 in kill-0$ pid means that no signal is sent to the process corresponding to PID, but the existence of the process corresponding to the variable value PID is still checked. If the process corresponding to $pid exists, 0 is returned, and there is no return 1.

2 > / dev/null does not output error messages.

The application scenario in the system is the script code snippet that stops the MySQL service in the / etc/init.d/mysqld script of MySQL.

The program code to shut down the database using the / etc/init.d/mysqld stop command to execute the script is as follows:

'stop')

# Stop daemon. We usea signal here to avoid having to know the

# root password.

Iftest-s "$mysqld_pid_file_path"

Then

Mysqld_pid= `cat "$mysqld_pid_file_path" `

If (kill-0 $mysqld_pid2 > / dev/null)

Then

Echo$echo_n "Shutting down MySQL"

Kill$mysqld_pid

# mysqld shouldremove the pid file when it exits, so wait for it.

Wait_for_pid removed "$mysqld_pid"$mysqld_pid_file_path"; return_value=$?

Else

Log_failure_msg "MySQL server process # $mysqld_pidis not running!"

Rm "$mysqld_pid_file_path"

Fi

# Delete lock forRedHat / SuSE

If test-f "$lock_file_path"

Then

Rm-f "$lock_file_path"

Fi

Exit$return_value

Else

Log_failure_msg "MySQLserver PID file could not be found!"

Fi

Tags: Code script process scene meaning application interpretation beginning boy signal command data database program system point value service check solution Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Microsoft Shulou Information Docker