编写linux服务启动脚本
2016-05-19 22:42
609 查看
1、编写服务脚本所用到的执行程序,此处编写一个python脚本文件myd,放置在/usr/sbin下,脚本内容文件如下:
2、编写服务脚本myd,放在/etc/init.d/目录下
3、执行service命令检验
4、通过chkconfig命令设置开机启动运行
<pre name="code" class="html">#!/usr/bin/env python import os from scapy.all import * pid= os.getpid() f = open('/var/run/myd.pid', 'w') f.write(str(pid)+ "\n") f.close() srloop(IP(dst='192.168.0.1')/ICMP())
2、编写服务脚本myd,放在/etc/init.d/目录下
<pre name="code" class="html">#!/bin/bash # # myd Start up the myd server daemon # # chkconfig: 2345 71 71 # description: This service starts up the myd server daemon. # # processname: myd # pidfile: /var/run/myd.pid ### BEGIN INIT INFO # Provides: myd # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start up the myd server daemon # Description: This service starts up the OpenSSH server daemon. ### END INIT INFO # source function library . /etc/rc.d/init.d/functions RETVAL=0 prog="myd" # Some functions to make the below more readable MYD="/usr/sbin/myd" PID_FILE=/var/run/myd.pid exec="python /usr/sbin/myd" runlevel=$(set -- $(runlevel); eval "echo \$$#" ) start() { [ -x $MYD ] || exit 5 MYDRUNNING=0 if [ -f "$PID_FILE" ]; then MYDPID=`cat "$PID_FILE" 2>/dev/null` if [ -n "$MYDPID" ] ; then MYDRUNNING=1 fi fi if [ $MYDRUNNING = 1 ] && [ $? = 0 ]; then # already running, do nothing action $"Starting $prog: " /bin/true RETVAL=0 else # prepare for start echo -n $"Starting $prog: " $MYD & RETVAL=$? if [ $RETVAL -ne 0 ]; then failure else success fi fi echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $PID_FILE RETVAL=$? echo } reload() { echo -n $"Reloading $prog: " killproc -p $PID_FILE $SSHD -HUP RETVAL=$? echo } restart() { stop start } case "$1" in start) start ;; stop) rm -f $lockfile stop ;; restart) restart ;; status) status -p $PID_FILE myd-daemon ;; *) echo $"Usage: $0 {start|stop|restart|status}" RETVAL=2 esac exit $RETVAL
3、执行service命令检验
<pre name="code" class="html">[root@localhost init.d]# service myd start 正在启动 myd: [确定]
<pre name="code" class="html">[root@localhost ~]# service myd status myd-daemon (pid 5658) 正在运行...
<pre name="code" class="html">[root@localhost ~]# service myd stop 停止 myd: [确定]
[root@localhost ~]# service myd status myd-daemon 已停
4、通过chkconfig命令设置开机启动运行
[root@localhost ~]# chkconfig --level 35 myd on
相关文章推荐
- linux 读取U盘的VID和PID
- Linux 常用的命令,更新中
- Linux驱动模型(六)
- Linux系统小知识汇总
- Linux命令——文件查找命令find
- archlinux设置屏幕亮度
- Linux下实现Qt 4.6.3移植 Ubuntu10.10篇--转载
- linux-----强大的find
- Linux——学习篇(二)之find命令
- find命令浅析
- Linux 下的指令
- Linux服务器安全策略
- Linux面试题(2)
- Linux面试题(1)
- linux基本命令(29)——chgrp命令
- archlinux配置BCM43142无线网卡
- 操作系统知识(2)--Linux 系统目录结构
- linux下各类服务器构建与使用指南
- 文件名查找指令find
- Linux及安全——模块