简体   繁体   English

Bash脚本命令未运行

[英]Bash script commands not running

I have seafile ( http://www.seafile.com/en/home/ ) running on my NAS and I set up a cron tab that runs a script every few minutes to check if the seafile server is up, and if not, it will start it 我的NAS上正在运行seafile( http://www.seafile.com/en/home/ ),并且设置了一个cron选项卡,该选项卡每隔几分钟运行一次脚本,以检查seafile服务器是否已启动,如果没有启动,它将开始

The script looks like this: 该脚本如下所示:

#!/bin/bash
# exit if process is running
if ps aux | grep "[s]eafile" > /dev/null
then exit
else
# restart process
/home/simon/seafile/seafile-server-latest/seafile.sh start
/home/simon/seafile/seafile-server-latest/seahub.sh start-fastcgi
fi

running /home/simon/seafile/seafile-server-latest/seafile.sh start and /home/simon/seafile/seafile-server-latest/seahub.sh start-fastcgi individually/manually works without a problem, but when I try to manually run this script file, neither of those lines execute and seafile/seahub do not start 分别/手动运行/home/simon/seafile/seafile-server-latest/seafile.sh start/home/simon/seafile/seafile-server-latest/seahub.sh start-fastcgi可以正常/无问题地工作,但是当我尝试要手动运行此脚本文件,这些行均不会执行,并且seafile / seahub均不会启动

Is there an error in my script that is preventing execution of those 2 lines? 我的脚本中是否有错误阻止了这两行的执行? I've made sure to chmod the script file to 755 我确保将脚本文件更改为755

The problem is likely that when you pipe commands into one another, you don't guarentee that the second command doesn't start before the first (it can start, but not do anything while it waits for input). 问题可能在于,当您将命令通过管道传递给彼此时,您无法确保第二条命令不会在第一条命令之前启动(它可以启动,但是在等待输入时什么也不做)。 For example: 例如:

oj@ironhide:~$ ps -ef | grep foo
oj        8227  8207  0 13:54 pts/1    00:00:00 grep foo

There is no process containing the word "foo" running on my machine, but the grep that I'm piping ps to appears in the process list that ps produces. 有包含单词“富”在我的机器上没有运行的过程,但grep ,我是管道ps到出现在进程列表ps产生。

You could try using pgrep instead, which is pretty much designed for this sort of thing: 您可以尝试使用pgrep代替,它是专门为这种事情而设计的:

if pgrep "[s]eafile"

Or you could add another pipe to filter out results that include grep : 或者,您可以添加另一个管道以过滤出包含grep结果:

ps aux | grep "[s]eafile" | grep -v grep

If the name of this script matches the regex [s]eafile it will trivially always take the exit branch. 如果此脚本的名称与正则表达式[s]eafile匹配, [s]eafile很简单地始终使用exit分支。

You should probably be using pidof in preference of reinventing the yak shed anyway. 无论如何,您可能应该优先使用pidof重塑in牛棚。

turns out the script itself was working ok, although the change to using pgrep is much nicer. 事实证明,脚本本身可以正常运行,尽管使用pgrep的更改要好得多。 the problem was actually in the crontab (didn't include the sh in the command) 问题实际上出在crontab中(命令中未包含sh)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM