简体   繁体   English

如何保持 bash 脚本在后台运行

[英]How to keep a bash script running in the background

I write a simple bash script:我写了一个简单的 bash 脚本:

while :
do
sleep 2;

//my code

done

Now I want this bash script always be running.现在我希望这个 bash 脚本一直在运行。

bash mybash.sh > /dev/null &

When I run above command my bash works fine.当我运行上面的命令时,我的 bash 工作正常。 but when I close my terminal I think my bash is killed.但是当我关闭终端时,我认为我的 bash 被杀死了。 because it doesn't work as my script make some files when it running.因为它不起作用,因为我的脚本在运行时会生成一些文件。

在终端中运行脚本“bash script.sh”并按 ctrl+z 然后使用 'bg' 命令将脚本置于后台

#!/bin/bash

while true; do

// your code

sleep 5;

done;

write a bash script and put it that to cron and check once it will start comment the cron it will run in a background.编写一个 bash 脚本并将其放入 cron 并检查一旦它开始评论它将在后台运行的 cron。

insted of sleep 5 you can use whatever second you want to put. insted of sleep 5 你可以使用任何你想要的秒数。

For checking your process use below commend to get the details ps -ef | grep script_file_name要检查您的进程,请使用以下命令来获取详细信息ps -ef | grep script_file_name ps -ef | grep script_file_name

if you find more then one process is running leave one process and rest kill the process for script.如果您发现更多然后一个进程正在运行,请离开一个进程并休息杀死脚本的进程。

Hope so this will resolve your issue....!!!!希望这能解决您的问题....!!!!

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

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