添加linux開機啟動任務


對於系統里面設置的開機啟動程序

先來看一個例子nginx啟動腳本

#!/bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
	. /etc/default/nginx
fi

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
	if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
		return 0
	else
		$DAEMON -t $DAEMON_OPTS
		return $?
	fi
}

case "$1" in
	start)
		echo -n "Starting $DESC: "
		test_nginx_config
		# Check if the ULIMIT is set in /etc/default/nginx
		if [ -n "$ULIMIT" ]; then
			# Set the ulimits
			ulimit $ULIMIT
		fi
		start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON -- $DAEMON_OPTS || true
		echo "$NAME."
		;;

	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON || true
		echo "$NAME."
		;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --quiet --pidfile \
		    /var/run/$NAME.pid --exec $DAEMON || true
		sleep 1
		test_nginx_config
		start-stop-daemon --start --quiet --pidfile \
		    /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
		echo "$NAME."
		;;

	reload)
		echo -n "Reloading $DESC configuration: "
		test_nginx_config
		start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON || true
		echo "$NAME."
		;;

	configtest|testconfig)
		echo -n "Testing $DESC configuration: "
		if test_nginx_config; then
			echo "$NAME."
		else
			exit $?
		fi
		;;

	status)
		status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
		;;
	*)
		echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
		exit 1
		;;
esac

exit 0

如果我們需要按照系統里面的格式創建文件我們可以使用sudo update-rc.d minidlna defaults來將程序加到默認的啟動程序里面, 在/etc/rcx.d里面將創建軟連接
如果遇到如下提示System start/stop links for /etc/init.d/minidlna already exist.說明這個文件已經存在,我們可以直接使用這個命令更新下sudo update-rc.d minidlna enable

使用rc.local 實現開機啟動

查看/etc/rcx.d文件夾里面的文件

root@Test1:/tmp/nginx# ls -l /etc/rc3.d/
total 4
-rw-r--r-- 1 root root 677 Jul 27  2012 README
lrwxrwxrwx 1 root root  17 Sep 23  2016 S10vboxadd -> ../init.d/vboxadd
lrwxrwxrwx 1 root root  27 Sep 23  2016 S20nfs-kernel-server -> ../init.d/nfs-kernel-server
lrwxrwxrwx 1 root root  21 Sep 23  2016 S30vboxadd-x11 -> ../init.d/vboxadd-x11
lrwxrwxrwx 1 root root  25 Sep 23  2016 S35vboxadd-service -> ../init.d/vboxadd-service
lrwxrwxrwx 1 root root  15 Sep 23  2016 S50rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root  19 Sep 23  2016 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx 1 root root  18 Sep 23  2016 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx 1 root root  14 Sep 23  2016 S75sudo -> ../init.d/sudo
lrwxrwxrwx 1 root root  21 Sep 23  2016 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root  18 Sep 23  2016 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root  18 Sep 23  2016 S99rc.local -> ../init.d/rc.local

可以看到這些都是鏈接文件,指向/etc/init.d文件夾
這里的S開頭的文件代表開機啟動,后兩位數字越小越先執行, 還有一個是K開頭的文件代表進入改運行級別時關閉程序,后兩位數字越小越先執行
注意最后一個文件S99rc.local,在各個運行基本都有,這是開機過程中最后運行的程序,可以將需要開機運行的程序寫到這里面vim /etc/rc.local

示例:開機時添加一條iptables規則

sh /home/ivan/iptables.sh 
echo 'Iptable Configured!'

同時注意需要確保你需要執行的程序寫在 exit 0的前面, 寫在之后將不起作用

使用定時任務crontab來曲線實現開機啟動

這里以crontab重啟時執行命令為例,通過crontab -e編輯自己的cron @reboot /path/to/script這將在重啟時運行.

使用upstart的開機啟動

upstart開機將會運行所有/etc/init目錄下的腳本(以.conf結尾的文件),這些程序將以root身份運行
同時upstart提供了~/.config/upstart文件,當用戶登陸時運行

一些桌面環境提供的自動運行

gnome和kde這些桌面程序也提供自動啟動程序,可以直接使用圖形化設置,也可以直接編輯配置文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM