新聞中心
作為一款開放性操作系統(tǒng),Linux通常會讓用戶優(yōu)化系統(tǒng),更好地使用其所提供的功能。自啟動程序是其中一項讓用戶感到方便的功能,因為只要配置好,用戶便可以在系統(tǒng)啟動后無需手動操作程序。在這篇文章中,我們將向大家分享Linux自啟動執(zhí)行程序的教程。

之一步:編寫需要自啟動的腳本
在開始配置自啟動程序之前,首先需要編寫需要自啟動的腳本。這個腳本可以是各種形式的文件,比如Python腳本、Shell腳本、或是其他的可執(zhí)行文件。本例中,我們以Shell腳本為例,以下是示例代碼:
#!/bin/bash
echo “hello world”
保存為example.sh文件。
第二步:將腳本放置到指定目錄
將剛剛編寫的腳本放置到以下目錄中的任意一個:
/etc/init.d/
/etc/rc.d/init.d/
/usr/local/etc/rc.d/
/usr/local/etc/rc.d/inetd/
/etc/network/if-up.d/
/etc/network/if-down.d/
/etc/ppp/ip-up.d/
/etc/ppp/ip-down.d/
/etc/apm/event.d/
/etc/acpi/events/
/etc/acpi/actions/
/etc/acpi/default.sh
第三步:授予權(quán)限
為了確保腳本可以被執(zhí)行,還需要授予腳本相應(yīng)的權(quán)限。在終端中,執(zhí)行以下命令,可以將剛剛創(chuàng)建的腳本賦予可執(zhí)行權(quán)限:
$ sudo chmod +x example.sh
第四步:添加啟動服務(wù)
在Linux系統(tǒng)中有一個init系統(tǒng),其主要作用是在系統(tǒng)啟動時運行開機(jī)腳本和服務(wù)。為了使腳本在系統(tǒng)啟動后自動啟動,我們需要將其加入到init系統(tǒng)的服務(wù)列表中。
將以下內(nèi)容復(fù)制到新建的/etc/init.d/example文件中:
#!/bin/sh
### BEGIN INIT INFO
# Provides: example
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start and Stop the Example Daemon
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Define daemon and set permissions
DAEMON=/usr/local/bin/example
NAME=example
DAEMON_OPTS=””
DAEMON_USER=root
# Choose user and group as well as log file name
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log
function check_for_root() {
if [ $(id -u) != 0 ]; then
printf “You have to be root to run this script.\n”
exit 1
fi
}
function check_for_daemon() {
if [ ! -x “$DAEMON” ]; then
printf “Executable $DAEMON does not exist, please check.\n”
exit 1
fi
}
function start() {
check_for_root
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo ‘Service already running’ >&2
return 1;
fi
echo ‘Starting service…’ >&2
local CMD=”$DAEMON $DAEMON_OPTS >> $LOGFILE & echo \$!”
sh -c “$CMD” > “$PIDFILE”
echo ‘Service started’ >&2
}
function stop() {
check_for_root
if [ ! -f “$PIDFILE” ] || ! kill -0 $(cat “$PIDFILE”); then
echo ‘Service not running’ >&2
return 1;
fi
echo ‘Stopping service…’ >&2
kill -15 $(cat “$PIDFILE”) && rm -f “$PIDFILE”
echo ‘Service stopped’ >&2
}
function status() {
check_for_root
if [ ! -f “$PIDFILE” ] || ! kill -0 $(cat “$PIDFILE”); then
echo ‘Service not running’ >&2
return 1;
fi
echo ‘Service running’ >&2
}
function reload() {
check_for_root
if [ ! -f “$PIDFILE” ] || ! kill -0 $(cat “$PIDFILE”); then
echo ‘Service not running’ >&2
return 1;
fi
echo ‘Reloading configuration…’ >&2
kill -HUP $(cat “$PIDFILE”) && echo ‘Service reloaded’ >&2
}
case “$1” in
start)
start
;;
stop)
stop
;;
status)
status
;;
reload)
reload
;;
*)
echo “Usage: $0 {start|stop|status|reload}”
esac
exit 0
在該文件中,以下參數(shù)需要進(jìn)行更改:
– DAEMON:需要啟動的腳本路徑。
– NAME:自定義服務(wù)名稱。
– DAEMON_USER:腳本需要運行的用戶。
– PIDFILE:記錄pid的文件的路徑和名稱。
– LOGFILE:記錄日志文件的位置和名稱。
– Required-Start和 Required-Stop:在此指定的其他服務(wù)必須在此服務(wù)之前啟動,才能確保所有先決條件均得到滿足。
– Default-Start和Default-Stop:自動啟動/停止級別。
第五步:啟動服務(wù)
在終端中執(zhí)行以下命令,即可啟動該服務(wù):
$ sudo service example start
打開日志文件,確保服務(wù)已成功啟動:
$ sudo tl -f /var/log/example.log
同時,也可以通過以下命令來停止服務(wù):
$ sudo service example stop
如果需要檢查當(dāng)前服務(wù)狀態(tài):
$ sudo service example status
如果要重新加載腳本設(shè)置,可以執(zhí)行以下命令:
$ sudo service example reload
現(xiàn)在,已經(jīng)成功地將Linux自啟動執(zhí)行程序添加到系統(tǒng)中。無論您何時啟動計算機(jī),該程序都會自動啟動,方便快捷。
相關(guān)問題拓展閱讀:
- 我想在linux系統(tǒng)中添加一個啟動項腳本,在系統(tǒng)啟動完后執(zhí)行,怎么做
我想在linux系統(tǒng)中添加一個啟動項腳本,在系統(tǒng)啟動完后執(zhí)行,怎么做
一般是0-6,比較常接觸的是
0-halt,系統(tǒng)直接關(guān)機(jī)
3-full user mode,完整模式,
6-reboot,重啟
所以你應(yīng)該在rc3.d下添加宏御這仿銀個腳本
S開頭的是,啟動系統(tǒng)時需要start的服務(wù)
K開頭的是,關(guān)機(jī)時需要關(guān)閉備絕宴stop的服務(wù)
S/K后面的數(shù)字表示執(zhí)行的順序
比如S10myservices/K10myservices
關(guān)于linux 啟動后執(zhí)行的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
本文名稱:Linux自啟動執(zhí)行程序教程(linux啟動后執(zhí)行)
當(dāng)前URL:http://www.5511xx.com/article/dhdppop.html


咨詢
建站咨詢
