1. 程式人生 > >Life is just like a dream.

Life is just like a dream.

有時候我們需要Linux系統在開機的時候自動載入某些指令碼或系統服務。

在解問題之前先來看看Linux的啟動流程

Linux的啟動流程

Linux啟動順序

主要順序就是:
1. 載入核心
2. 啟動初始化程序
3. 確定執行級別
4. 載入開機啟動程式
5. 使用者登入

第4步載入啟動程式其實是兩步:

  1. init程序逐一載入開機啟動程式,其實就是執行指定目錄裡的啟動指令碼。
  2. 在執行完指定目錄裡面的程式後init程序還會去執行/etc/rc.local 這個指令碼。

ps:“指定目錄”是指在第3步中設定的執行級別對應的目錄。

要完成我們的需求,我們使用第4步中的任意一種方式都可以。

下面分別就是這兩種方式的具體實現:

1.chkconfig

#!/bin/sh
##
## /etc/rc.d/init.d/supervisord
##
#supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
# Source init functions . /etc/rc.d/init.d/functions prog="supervisord" prefix="/usr/" exec_prefix="${prefix}" PIDFILE="/var/run/supervisord.pid" CONFIG="/etc/supervisord.conf" prog_bin="${exec_prefix}bin/supervisord -c $CONFIG " function log_success_msg() { echo "[email protected]" "[ OK ]" } function log_failure_msg() { echo "
[email protected]
"
"[ OK ]" } start() { #echo -n $"Starting $prog: " #daemon $prog_bin --pidfile $PIDFILE #[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog failed" #echo if [ ! -r $CONFIG ]; then log_failure_msg "config file doesn't exist (or you don't have permission to view)" exit 4 fi if [ -e $PIDFILE ]; then PID="$(pgrep -f $PIDFILE)" if test -n "$PID" && kill -0 "$PID" &>/dev/null; then # If the status is SUCCESS then don't need to start again. log_failure_msg "$NAME process is running" exit 0 fi fi log_success_msg "Starting the process" "$prog" daemon $prog_bin --pidfile $PIDFILE log_success_msg "$prog process was started" } stop() { echo -n $"Shutting down $prog: " [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown" echo } case "$1" in start) start ;; stop) stop ;; status) status $prog ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart|status}" ;; esac

第1步:把上面的指令碼放在/etc/init.d/資料夾下。

ln -s ./supervisord  /etc/init.d/supervisord

第2步:將啟動指令碼許可權改為可執行。

chmod a+x /etc/init.d/supervisord

第3步:新增啟動項。

chkconfig --add supervisord
chkconfig supervisord on

第4步:檢查是否設定成功。

chkconfig --list | grep supervisord
supervisord     0:關閉    1:關閉    2:啟用    3:啟用    4:啟用    5:啟用    6:關閉

成功~

2.修改/etc/rc.local指令碼

/etc/rc.local 指令碼內容如下

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

#touch /var/lock/subsys/local
echo "hello linux" >> /tmp/hello2.log

influxd > /tmp/influxd.log 2>&1 &

echo "hello linux" >> /tmp/hello3.log

echo “hello linux” >>/tmp/hello2.log ,就模擬了一個簡單的開機啟動指令碼。

influxd 則就是啟動 influxd 服務。

ps: influxd > /tmp/influxd.log 2>&1 & 這樣寫的意思是讓influxd後臺執行。

influxd和前面的echo "hello linux"是不一樣的,echo 執行過後就結束了,而influxd則為服務一直執行,如果不後臺執行的話則influxd 啟動後就不會返回,那麼init程序就會一直等待influxd執行完畢,導致後面的程式一直無法執行。

這個著實坑了我一把,當時我寫的是:

#!/usr/bin/python
...
influxd
telegraf

發現influxd啟動成功了,telegraf就是起不來。後來把telegraf寫在前面就能起來,但是influxd又起不來了於是就猜測是這個原因~~~bingo。

相關推薦

Life is just like a dream.

有時候我們需要Linux系統在開機的時候自動載入某些指令碼或系統服務。 在解問題之前先來看看Linux的啟動流程 Linux的啟動流程 主要順序就是: 1. 載入核心 2. 啟動初始化程序 3. 確定執行級別 4. 載入開機啟動程式

【[&] () {}】Life is short , Import your DREAM.獨學無友,則孤陋而難成;久處一方,則習染而不自覺。

專業軟體工程, 現全職 iOS 開發工程師, 業餘 Android/遊戲開發愛好者 專注於C/C++, 移動應用開發,特別是移動遊戲的開發。Lisp語言的死忠粉:),熱衷於計算機底層原理。 連結我: 我的新浪部落格 Q:1020935219 微訊號:C_W_hu

Facebook Is Just Like the NSA

Last year, in the pages of New York, Max Read wondered whether Facebook’s founder and CEO, Mark Zuckerberg, really had any idea what his platform actually

Google's Home Hub is more like a Chromecast than an Android tablet

At first glance, Google's new Home Hub looks like any other smart display featuring the company's Assistant platform. It features both voice and touch inpu

[&] () {}(Life is short , Import your DREAM.獨學無友,則孤陋而難成;久處一方,則習染而不自覺。)

專業軟體工程, 現全職 iOS 開發工程師, 業餘 Android/遊戲開發愛好者 專注於C/C++, 移動應用開發,特別是移動遊戲的開發。Lisp語言的死忠粉:),熱衷於計算機底層原理。 連結我: 我的新浪部落格 Q:1020935219 微訊號:C_W_hu

JAVA基礎——is-a 、have-a、和 like-a的區別

就是 不同類 alike 不同 pc機 空調 面向 們的 合成 1、is-a,has-a,like-a是什麽 在面向對象設計的領域裏,有若幹種設計思路,主要有如下三種: is-a、has-a、like-a java中在類、接口、抽象類中有很多體現。 了解java看這裏:什麽

Is-a、has-aLike-a區別

Is-a: 是a:A Is B:A是B(繼承關係,繼承)。 has-a: 有a:A has B:A有B(從屬關係,聚合)。 like-a: 像a:A like B:A像B(組合關係,介面)。 關於Is-a、has-a、like-a的使用場景: 如果A,B是Is-

類中is-a、has-ais-like-a的區別

在此先做一個極為簡單的描述,有待補充。 一、is-a概念 is-a的意思是說:子類即父類。也就是子類在繼承父類之後,並沒有做任何異於父類的操作,比如並未新增新的內容。 二、has-a概念 has-a說的是一種包含關係,意思是說父類包含子類,比如人和心臟的關係,心臟屬於人,但心

life like a bowl of soup

It seeming ,only liked a small obstacle for my life. if life look like a bowl of soup,sometimes it become salty,although we will fe

java 面向物件關係is-a has-a like-a區別

java 面向物件關係is-a has-a like-a 轉載自:https://blog.csdn.net/qq_23835497/article/details/75333350 1.is-a is-a,顧名思義,是一個,代表繼承關係。 如果A is-a B

OOP思想> IS-AIS-LIKE-A,HAS-A的區別

IS-A 代表類與類之間的繼承關係 在知識表示、面向物件程式設計與面向物件設計的領域裡,is-a英語,包含架構)指的是類的父子繼承關係,例如類D是另一個類B的子類(類B是類D的父類)。 換句話說,通常”Dis aB”(B把D包含在內,或是D被包含在B內)指的是,概念體D

【天道酬勤】Mama said, life's like a box of chocolate, you never know what you gonna get next.

天道酬勤 Mama said, life's like a box of chocolate, you never know what you gonna get next.

Ask HN: What is it like to run a VPN as a business?

Hello,I'm going to set up my own VPN on some cloud hosting provider, and I'm toying with the idea of turning it into a small business.Is there anything I n

Kaspars Grosu on LinkedIn: "This is happening now it's not a dream not even Science fiction #innovation #tech #ai #tesla "

This is happening now it's not a dream not even Science fiction #innovation #tech #ai #tesla Friday 7 September 2018 Real life incident.. What happens whe

Movin’ Data is a Dream

Search is a big feature at Shaadi.com. It helps our matchmaking algorithms bring out the best matches and it gets our customers closer to finding their par

Magic Leap's Mica AI Is Like A 21st Century Rorschach Test

Magic Leap introduced a concept called Mica and called it "her" during a section of its 3-hour keynote this week about how an artificial intelligence …

Trust Is Just A Well-Kept Record

There is a reason the stone of Hammurabi, with its hundreds of lines of legal and economic precedents, was made of, well, stone. You couldn’t easily alter

Remember, Bitcoin Is Just a Game

No one invests in the lottery; they play it. Rather than investing in bitcoin, let’s play bitcoin.On December 8, 2017, just a few days after bitcoin crosse

Sentiment Analysis: Thanks to Artificial Intelligence, it’s no Longer Just a Dream

Until a few years ago, being able to monitor and analyze sentiment in all the conversations taking place on blogs, forums

OOP程式設計之is-a、has-ais-like-a之間的關係

概要:本文主要討論is-a和has-a以及is-a和is-like-a之間的關係。 OOP程式設計思想中,繼承是一個十分重要的內容。下面介紹幾個 常見的容易混淆的術語。 首先舉例子: 我們常見的動物,羊有很多種類,只是一種泛指,綿羊是其中一種,那麼羊和綿羊的關係,就是is-