Linux添加用户启动项

对于Debian系统,简单地运行自启动项可以在

/etc/rc.local

文件中直接添加代码


若需要服务的启动和停止操作,则应在/etc/init.d 文件夹内添加脚本,以服务名作为文件名,例如

#! /bin/sh
  # /etc/init.d/vncboot

  ### BEGIN INIT INFO
  # Provides: vncboot
  # Required-Start: $remote_fs $syslog
  # Required-Stop: $remote_fs $syslog
  # Default-Start: 2 3 4 5
  # Default-Stop: 0 1 6
  # Short-Description: Start VNC Server at boot time
  # Description: Start VNC Server at boot time.
  ### END INIT INFO

  USER=pi
  HOME=/home/pi

  export USER HOME

  case "$1" in
   start)
    echo "Starting VNC Server"
    #Insert your favoured settings for a VNC session
    su - $USER -c "/usr/bin/vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565"
    ;;

   stop)
    echo "Stopping VNC Server"
    /usr/bin/vncserver -kill :1
    ;;

   *)
    echo "Usage: /etc/init.d/vncboot {start|stop}"
    exit 1
    ;;
  esac

  exit 0

或者复杂些的

#!/bin/bash

# The following part always gets executed.
echo "This part always gets executed"

## Fill in name of program here.
PROG="pi"
PROG_PATH="/usr/bin" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
PROG_ARGS="10000000" 
PID_PATH="/var/run/"

start() {
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        echo "Error! $PROG is currently running!" 1>&2
        exit 1
    else
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.
            $PROG_PATH/$PROG $PROG_ARGS 2>&1 >/dev/null &
        echo "$PROG started"
        touch "$PID_PATH/$PROG.pid"
    fi
}

stop() {
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, so stop it
        killall $PROG

        rm "$PID_PATH/$PROG.pid"

        echo "$PROG stopped"
    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    fi
}

###Check to see if we are running as root first.
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
    ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
    ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

然后,添加入自启动项

sudo chmod 755 /etc/init.d/foobar
sudo update-rc.d foobar defaults

移除可以使用命令

sudo update-rc.d -f foobar remove

另一个启动项统一管理工具是chkconfig,


用户级的启动项可在

/etc/profile.d/

文件夹内添加.sh文件


LXDE图形界面的自启动文件可编辑

/etc/xdg/lxsession/LXDE/autostart 
~/.config/lxsession/LXDE/autostart
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值