Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何终止名为“的所有进程”;“shairport”;没有pid 12345的_Linux_Bash - Fatal编程技术网

Linux 如何终止名为“的所有进程”;“shairport”;没有pid 12345的

Linux 如何终止名为“的所有进程”;“shairport”;没有pid 12345的,linux,bash,Linux,Bash,我在工作中使用shairport来播放音乐。我在Debian机器(树莓)上运行它。在/etc/init.d/shairport文件中,它只有start | stop命令。 我想添加一个重启。以下是迄今为止的代码: case "$1" in restart) service shairport stop service shairport start ;; start) /usr/local/bin/shairport -d -a "$NAME" -p 50

我在工作中使用shairport来播放音乐。我在Debian机器(树莓)上运行它。在/etc/init.d/shairport文件中,它只有start | stop命令。 我想添加一个重启。以下是迄今为止的代码:

case "$1" in
  restart)
    service shairport stop
    service shairport start
    ;;
  start)
    /usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
    ;;
  stop)
    killall shairport
    ;;
  *)
    echo "Usage: /etc/init.d/shairport {start|stop|restart}"
    exit 1
    ;;
esac

exit 0
问题是,当我运行“serviceshairport restart”时,服务被停止,从而运行“killall shairport”并终止bash脚本进程本身。所以“开始”永远不会执行

如何使killall杀死除当前脚本之外的所有shairport


我的想法是获取pid并将其排除,但我找不到如何做到这一点。

开始部分应记录已启动流程的pid。 类似于
echo$?>/var/run/shairport.pid
就可以了


脚本的Stop部分将使用我们创建的.PID文件中的PID,并终止正确的进程。据我所知,大多数Linux服务都是这样做的。

开始部分应该写下已启动进程的PID。 类似于
echo$?>/var/run/shairport.pid
就可以了


脚本的Stop部分将使用我们创建的.PID文件中的PID,并终止正确的进程。据我所知,大多数Linux服务都是这样做的。

在Linux中,您可以知道脚本文件运行的进程ID:$$

你只需检查一下,你没有用以下方式自杀:

stop)
for pid in $(pgrep shairport); do
    if[$pid != $$]
        kill $pid
    fi
done

在linux中,您可以知道脚本文件运行的进程ID:$$

你只需检查一下,你没有用以下方式自杀:

stop)
for pid in $(pgrep shairport); do
    if[$pid != $$]
        kill $pid
    fi
done

由于我选择的答案并非一目了然,以下是我最终得出的代码:

case "$1" in
  restart)
    for pid in $(pgrep shairport); do
            if [ "$pid" != $$ ]; then
                kill $pid
            fi
    done
    /usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
    ;;
  start)
    /usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
    ;;
  stop)
        killall shairport
    ;;
  *)
    echo "Usage: /etc/init.d/shairport {start|stop|restart}"
    exit 1
    ;;
esac

由于我选择的答案并非一目了然,以下是我最终得出的代码:

case "$1" in
  restart)
    for pid in $(pgrep shairport); do
            if [ "$pid" != $$ ]; then
                kill $pid
            fi
    done
    /usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
    ;;
  start)
    /usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
    ;;
  stop)
        killall shairport
    ;;
  *)
    echo "Usage: /etc/init.d/shairport {start|stop|restart}"
    exit 1
    ;;
esac

需要在“服务器故障”中询问此问题。为什么不能通过发送stop命令而不是终止进程来停止服务?需要在“服务器故障”中询问此问题。为什么不能通过发送stop命令而不是终止进程来停止服务?