将脚本制作成linux服务

– 编辑,还有一些问题 –

好我的脚本使用循环来等待网络连接.因此,当我运行它时,即使使用守护进程,它也只是坐在那里而不是让我回到shell.我试过su -c“/ home / webreports / report-list&” USER但它试图以用户身份运行&即使我有引号,我甚至尝试过单引号.

– 原版的 –
我已经制作了一个脚本(尚未测试),用于将bash脚本作为服务运行.我有两个问题.

1)如何让它作为特定用户运行?我们使用的软件不能以root身份运行,并且如果它确实会崩溃(可怕的软件我们很遗憾).那么如何让用户“JOEBOB”让它运行服务呢.

2)我是否只将脚本文件放入“/etc/rc5.d”以便能够使用“service report-listen start”?

—脚本 –

#!/bin/sh
#
# myservice     This shell script takes care of starting and stopping
#               the /home/webreports/report-listen
#

# Source function library
. /etc/rc.d/init.d/functions


# Do preliminary checks here,if any
#### START of preliminary checks #########


##### END of preliminary checks #######


# Handle manual control parameters like start,stop,status,restart,etc.

case "$1" in
  start)
    # Start daemons.

    echo -n $"Starting report-listen daemon: "
    echo
    daemon /home/webreports/report-listen
    echo
    ;;

  stop)
    # Stop daemons.
    echo -n $"Shutting down report-listen: "
    killproc /home/webreports/report-listen
    echo

    # Do clean-up works here like removing pid files from /var/run,etc.
    ;;
  status)
    status /home/webreports/report-listen

    ;;
  restart)
    $0 stop
    $0 start
    ;;

  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit 0

解决方法

使用su以不同的用户身份运行脚本:
daemon su -c /home/webreports/report-listen johndoe

其中johndoe是您希望它运行的用户.

将脚本放在/etc/init.d/myservice中,然后将其符号链接到/etc/rc.d/S99myservice.

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...