Get the App
SLTechnology News&Howtos  ›  Development  › 

How to register Shell script with Linux system service

Shulou Source: shulou.com Published: 2022-06-03 09:25:30 11月15日 Update

This article introduces the relevant knowledge of "how to register Shell scripts to Linux system services". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Register a system service and boot automatically.

1 scripting

# vim test.sh

The code is as follows:

#! / bin/bash

# description: hello.sh

# chkconfig: 2345 20 81

EXEC_PATH=/usr/local/

EXEC=hello.sh

DAEMON=/usr/local/hello.sh

PID_FILE=/var/run/hello.sh.pid

. / etc/rc.d/init.d/functions

If [!-x $EXEC_PATH/$EXEC]; then

Echo "ERROR: $DAEMON not found"

Exit 1

Fi

Stop ()

{

Echo "Stoping $EXEC..."

Ps aux | grep "$DAEMON" | kill-9 `awk'{print $2}'`> / dev/null 2 > & 1

Rm-f $PID_FILE

Usleep 100

Echo "Shutting down $EXEC: [OK]"

}

Start ()

{

Echo "Starting $EXEC..."

$DAEMON > / dev/null &

Pidof $EXEC > $PID_FILE

Usleep 100

Echo "Starting $EXEC: [OK]"

}

Restart ()

{

Stop

Start

}

Case "$1" in

Start)

Start

Stop)

Stop

Restart)

Restart

Status)

Status-p $PID_FILE $DAEMON

*)

Echo "Usage: service $EXEC {start | stop | restart | status}"

Exit 1

Esac

Exit $?

2 Registration service

The code is as follows:

# chmod 700 test.sh

# cp test.sh / etc/init.d/

# chkconfig-add test.sh

# chkconfig-list

3. Delete a service

The code is as follows:

# chkconfig-del test.sh

This is the end of the content of "how to register the Shell script to the Linux system service". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Services systems scripts code content more knowledge practicality learning next dilemma reality situation articles cases editing websites industries processes high quality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Huawei Linux MariaDB