前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >How to Write Standard Startup Script

How to Write Standard Startup Script

作者头像
happy123.me
发布2018-06-04 12:21:25
4250
发布2018-06-04 12:21:25
举报
文章被收录于专栏:乐享123乐享123

centos6中的init.d systemV script升级为systemd startup script,让我们有个easy setup的模板…

模板实例:

systemV init script template

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

#!/bin/bash # testclient - Startup script for testclient # chkconfig: 35 85 15 # description: testclient is your openstack VMS monitor and ovs auto config bot. # processname: testclient # config: /etc/testclient.conf . /etc/rc.d/init.d/functions # NOTE: if you change any OPTIONS here, you get what you pay for: # this script assumes all options are in the config file. CONFIGFILE="/etc/testclient.conf" testCLIENT=/usr/local/bin/testclient testCLIENT_USER=helloworldtest testCLIENT_GROUP=helloworldtest # things from testclient.conf get there by testclient reading it PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidFilePath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | aw PIDDIR=`dirname $PIDFILEPATH` LOGFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?logFilePath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | aw LOGDIR=`dirname $LOGFILEPATH` OPTIONS=" -c $CONFIGFILE" start() { # Make sure the default pidfile directory exists if [ ! -d $PIDDIR ]; then install -d -m 0755 -o $testCLIENT_USER -g $testCLIENT_GROUP $PIDDIR fi if [ ! -d $LOGDIR ]; then install -d -m 0755 -o $testCLIENT_USER -g $testCLIENT_GROUP $LOGDIR fi echo -n $"Starting testclient: " daemon --pidfile "$PIDFILEPATH" --user "$testCLIENT_USER" --check $testCLIENT "$testCLIENT $OPTIONS >$LOGFILEPATH 2>&1 &" RETVAL=$? pid=`ps -A x | grep $testCLIENT | grep -v grep | cut -d" " -f1 | head -n 1` if [ -n "$pid" ]; then echo $pid > $PIDFILEPATH fi [ $RETVAL -eq 0 ] && touch /var/lock/subsys/testclient echo return $RETVAL } stop() { echo -n $"Stopping testclient: " testclient_killproc "$PIDFILEPATH" $testCLIENT RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/testclient } restart () { stop start } # Send TERM signal to process and wait up to 300 seconds for process to go away. # If process is still alive after 300 seconds, send KILL signal. # Built-in killproc() (found in /etc/init.d/functions) is on certain versions of Linux # where it sleeps for the full $delay seconds if process does not respond fast enough to # the initial TERM signal. testclient_killproc() { local pid_file=$1 local procname=$2 local -i delay=10 local -i duration=1 local pid=`pidofproc -p "${pid_file}" ${procname}` kill -TERM $pid >/dev/null 2>&1 usleep 1000 local -i x=0 while [ $x -le $delay ] && checkpid $pid; do sleep $duration x=$(( $x + $duration)) done kill -KILL $pid >/dev/null 2>&1 usleep 1000 checkpid $pid # returns 0 only if the process exists local RC=$? [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown" RC=$((! $RC)) # invert return code so we return 0 when process is dead. return $RC } RETVAL=0 case "$1" in start) start ;; stop) stop ;; restart|reload|force-reload) restart ;; condrestart) [ -f $PIDFILEPATH] && restart || : ;; status) status $testCLIENT RETVAL=$? ;; *) echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" RETVAL=1 esac exit $RETVAL

systemd startup script template

helloworld.conf

1 2 3 4 5

node = 1 [system] pidFilePath=/var/lib/helloworldtest/run/hello.pid logFilePath=/var/lib/helloworldtest/log/hello.log

/usr/lib/systemd/system/helloworld.service

1 2 3 4 5 6 7 8 9 10 11 12 13 14

[Unit] Description=helloworld [Service] Type=forking EnvironmentFile=/etc/helloworld.conf ExecStartPre=/bin/sh -c '/bin/install -d -m 0755 -o root -g root $( /usr/bin/dirname ${logFilePath} )' ExecStartPre=/bin/sh -c '/bin/install -d -m 0755 -o root -g root $( /usr/bin/dirname ${pidFilePath} )' ExecStart=/usr/local/sbin/daemonize -p ${pidFilePath} -a -o ${logFilePath} -e ${logFilePath} /usr/local/bin/helloworld -c /etc/helloworld.conf ExecStopPost=/bin/kill $MAINPID Restart=always [Install] WantedBy=default.target

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • systemV init script template
  • systemd startup script template
    • helloworld.conf
      • /usr/lib/systemd/system/helloworld.service
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档