庆典的cron羊群屏幕 [英] bash cron flock screen

查看:176
本文介绍了庆典的cron羊群屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cron来定期运行一个bash脚本,并尝试使用羊群以prevent这个脚本,它从创建过程中运行多次。

I am using cron to run a bash script periodically, and trying to use flock to prevent this script and the processes it creates from being run multiple times.

在crontab中的条目来运行它每分钟是:

The entry in crontab to run it every minute is:

*/1 * * * * flock -n /tmp/mylockfile /home/user/myscript.sh arg1 arg2

问题是,myscript.sh派生多个屏幕会议在独立模式下,它包含

The problem is, myscript.sh spawns multiple screen sessions in detached mode, it contains

for i in {1..3}; 
do 
    screen -d -m ./mysubscript.sh arg3
done

运行屏幕-d -m如上开始屏幕分离模式派生进程,但这些进程不继承羊群锁,使每分钟3新画面进程中运行mysubscript.sh现身。

Running screen with "-d -m" as above starts screen in "detached" mode as forked process, but these processes do not inherit the lock from flock, so that every minute 3 new screen processes running mysubscript.sh show up.

如果我用-D -m,而不是那么只有一个屏幕的进程中运行的所有时间,直到完成mysubscript.sh,而不是三个。

If I use "-D -m" instead then only one screen process runs all the time until mysubscript.sh finishes, not three.

我需要的是蜂拥而至,只运行myscript.sh如果没有运行mysubscript.sh的屏幕进程正在运行。

What I need is flock to only run myscript.sh if none of the the screen processes running mysubscript.sh are running.

如何才能实现这一目标?有没有在屏幕或羊群的标志,可以帮助实现这一目标?

How can this be achieved? Are there flags in screen or flock that can help to achieve this?

编辑:如果我改变线内的for循环插入运行mysubscript.sh与后台进程:

If I change the line inside the for loop into running mysubscript.sh as a background process with:

./mysubscript.sh arg3 &

锁定行为是完全按照我想要的,只是我没有单独的屏幕了。

The locking behavior is exactly as I want, except that I do not have the separate screens anymore.

推荐答案

根据您的具体需求,你可以运行你的下标循环连续的所有或每个创建单独的屏幕上会。

Depending on your exact needs you can either run your subscript loop all consecutively or create individual screen sessions for each.

screens.sh

#!/bin/bash

if ! screen -ls | grep -q "screenSession"
    then
        for i in {1..3};
            do
                screen -S screenSession_${i} -d -m sh mysubscript.sh arg3 &
            done
            { echo "waking up after 3 seconds"; sleep 3; } &
            wait # wait for process to finish
            exit # exit screen
    else echo "Screen Currently Running"
fi

会话

62646.screenSession_1   (Detached)
62647.screenSession_2   (Detached)
62648.screenSession_3   (Detached)

这方法会为每个循环迭代设置屏幕并执行标。如果发生了cron的尝试和运行脚本,同时还有活动插座,然后将退出到明年的cron。

This method will setup screens for each of iteration of your loop and execute the subscript. If cron happens to try and run the script while there are still active sockets then it will exit until next cron.

screencaller.sh

#!/bin/bash

if ! screen -ls | grep -q "screenSession"
    then screen -S screenSession -d -m sh screen.sh
    else echo "Screen Currently Running"
fi

screen.sh

#!/bin/bash

for i in {1..3}; 
do 
    sh mysubscript.sh arg3
done

{ echo "waking up after 3 seconds"; sleep 3; } &

wait # wait for process to finish
exit # exit screen

会议

59916.screenSession (Detached)

此方法使用一个单独的呼叫者脚本,然后简单地循环在同一屏幕会议后,其他的标之一。

This method uses a separate caller script then simply loops your subscript one after the other in the same screen session.

无论哪种方法,然后将像这样执行(如screens.sh,或screencaller.sh):

Either method would then be executed like so (eg. screens.sh, or screencaller.sh):

*/1 * * * * flock -n /tmp/mylockfile screens.sh arg1 arg2

或者,如果你想从CLI手动运行它只是做:

Or if you wanted to run it manually from CLI just do:

$ sh screens.sh

如果你想进入你只需拨打屏幕-r screenSession 会话。一对夫妇的其他有用的命令是屏幕-ls 的ps aux | grep的屏幕这表明您当前正在运行的屏幕和流程。

If you wanted to enter the session you would just call screen -r screenSession. A couple of other useful commands are screen -ls and ps aux | grep screen which shows the screens and processes that you are currently running.

这篇关于庆典的cron羊群屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆