对于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