添加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这些桌面程序也提供自动启动程序,可以直接使用图形化设置,也可以直接编辑配置文件

转载于:https://www.cnblogs.com/mikeguan/p/6720786.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值