我有一个Shell脚本,该脚本计划使用crontab条目每分钟运行。
脚本检查Java进程是否已启动。如果该过程不启动,它将尝试在下面启动Java过程,
这是Shell脚本
#!/bin/bash
val=0
val=$(ps -efa | grep -v grep | grep import -c)
echo ${val} >> /software/deployment/service/import2/import-integrator.log
if [[ ${val} -eq 4 ]]; then echo "Import Up" >> /tmp/output.log
else
echo "Import Down" >> /tmp/output.log
cd /software/deployment/service/import2/
nohup /usr/lib/jvm/java-11-openjdk-amd64/bin/java -jar -Xms4096m -Xmx16384m /software/deployment/service/import2/import-web-1.0-SNAPSHOT.war >> /software/deployment/service/import2/import-integrator.log &
echo "Import Started" >> /tmp/output.log
fi
上面的脚本使用以下命令(使用 root 用户帐户),
crontab -e
下面是
*/1 * * * * /bin/bash /software/deployment/service/import2/restart_import.sh
该条目。 val的值变量的值为 4 (GREP的匹配模式计数) 如果该过程已上升如果该过程下降,它将如 3 。 当Cron自动执行脚本
val 时,就会发生这种情况,如下所示,
val=$(ps -efa | grep -v grep | grep import -c)
如果脚本是手动执行的,则 val的值 is > 1 当该过程启动并且该过程下降时它为 0 。
我想了解为什么会发生这种情况(手动执行相同的命令,并通过cron返回两个不同的值)
I have a shell script which is scheduled to run every minute using a crontab entry.
The script checks if the Java process is up. If the process is not up, it will try to bring up the Java process
Below is the shell script
#!/bin/bash
val=0
val=$(ps -efa | grep -v grep | grep import -c)
echo ${val} >> /software/deployment/service/import2/import-integrator.log
if [[ ${val} -eq 4 ]]; then echo "Import Up" >> /tmp/output.log
else
echo "Import Down" >> /tmp/output.log
cd /software/deployment/service/import2/
nohup /usr/lib/jvm/java-11-openjdk-amd64/bin/java -jar -Xms4096m -Xmx16384m /software/deployment/service/import2/import-web-1.0-SNAPSHOT.war >> /software/deployment/service/import2/import-integrator.log &
echo "Import Started" >> /tmp/output.log
fi
The above script is schedule using below command (using root user account)
crontab -e
Below is the entry
*/1 * * * * /bin/bash /software/deployment/service/import2/restart_import.sh
The problem is that the value of val variable is coming as 4 (count of matching pattern by grep) if the process is up and it is coming as 3 if the process is down. This happens when the script is executed automatically by cron
val is evaluated as below
val=$(ps -efa | grep -v grep | grep import -c)
If the script is executed manually, the value of val is 1 when the process is up and it is 0 when the process is down.
I want to understand why this is happening (execution of same command manually and via cron returning two different values)
发布评论
评论(1)
遵循cron的脚本的逻辑:
第1分钟
假设
$ {val}
== 0脚本运行:
分钟2
假设
$ {val}
== 1脚本运行:
分钟3
假设
$ {val}
== 2脚本运行:
第4分钟
假设
$ {val}
== 3脚本运行:
第5分钟,
假设
$ {val} < /code> == 4
脚本运行:
第6分钟
假设
$ {val}
== 4脚本运行:
Following the logic of your script with cron:
minute 1
Assuming
${val}
== 0Script runs:
minute 2
Assuming
${val}
== 1Script runs:
minute 3
Assuming
${val}
== 2Script runs:
minute 4
Assuming
${val}
== 3Script runs:
minute 5
Assuming
${val}
== 4Script runs:
minute 6
Assuming
${val}
== 4Script runs: