1)创建 /etc/init.d/example 具有此类内容的脚本:
/etc/init.d/example
#!/bin/sh ### BEGIN INIT INFO # Provides: Example APP # Required-Start: # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 1 0 6 # Short-Description: ... # Description: ... ### END INIT INFO APP_NAME="Example APP" APP_DIR="/path/to/example" USER="example" GROUP="example" USAGE="Usage: $0 {start|stop|restart|status}" start_app() { echo "Starting $APP_NAME ..." su - $USER -c "cd $APP_DIR && ./example" } stop_app() { echo "NOT IMPLEMENTED" } status_app() { echo "NOT IMPLEMENTED" } restart_app() { echo "NOT IMPLEMENTED" } case "$1" in start) start_app ;; stop) stop_app ;; restart) restart_app ;; status) status_app ;; *) echo $USAGE exit 1 ;; esac
或者干脆:
#!/bin/sh ### BEGIN INIT INFO # Provides: Example APP # Required-Start: # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 1 0 6 # Short-Description: ... # Description: ... ### END INIT INFO APP_NAME="Example APP" APP_DIR="/path/to/example" USER="example" GROUP="example" USAGE="Usage: $0 {start}" start_app() { echo "Starting $APP_NAME ..." su - $USER -c "cd $APP_DIR && ./example" } case "$1" in start) start_app ;; *) echo $USAGE exit 1 ;; esac
2)给出默认值并启用:
cd /etc/init.d chmod +x example update-rc.d example defaults update-rc.d example enable