微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将脚本制作成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.

原文地址:https://www.jb51.cc/linux/399616.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐