Bash Cron Flock Screen

bash cron flock screen

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

Multiple Screen Method

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

Sessions

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

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.

Single Screen Caller Method

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

Session

59916.screenSession (Detached)

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

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

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

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

$ sh screens.sh

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.

Need specific cron job explained that uses flock -n + .lock file + /bin/bash

flock -n LOCK-FILE COMMAND

In your case:

  • -n: if flock cannot obtain the LOCK-FILE, i.e. it already exists, it will stop right there, and not execute the COMMAND.
  • So the -n ensures only one instance of COMMAND can run at a time.
  • in your case LOCK-FILE is /var/www/vhosts/blia.preview.be/scripts/blia_bab_import.sh.lock
  • The LOCK-FILE is what you decide it to be. flock will check for that file as a lock. It could be named anything, anywhere on your system, but doing it this way is nice since you know what the .lock file is used for.

    • To illustrate, you could do flock -n /tmp/some_lock_file.txt script.sh. The value is not enforced, you decide what you want to use.
  • COMMAND in your case is /bin/bash /var/www/vhosts/blia.preview.be/scripts/blia_bab_import.sh
  • The script you want to execute is the value of COMMAND, so again for you: /bin/bash /var/www/vhosts/blia.preview.be/scripts/blia_bab_import.sh
  • /bin/bash is to specify that the script blia_bab_import.sh must run inside a bash shell. You could do without by using the #!/bin/bash first line in your .sh file.

What is the correct syntax for flock with bash options in cron

No, this is not correct. See man crontab for syntax of the crontab files. The correct command would look like:

7,22,37,52 6-16 * * *  myuser  /bin/bash -l -c "cd /to/my/path/ && flock -n /var/run/my_app.lock -c execute_my_command"

..but a cleaner approach is to put it into a wrapper script and run that script from cron.

Resume last screen, if any

You can already do this with existing screen options without explicitly checking for a named session. screen -D -R serverman will attach to serverman if exists, but will create it and attach to it if not.

Timer Event in Bash

I think you tried out wrong if...then condition.
Here is my shot at it ... and it should send the "special command" only once at the required time.

#!/bin/bash

# Set the "next time" when special command should be sent
((start=SECONDS+300))

while read LINE <&3; do
echo "do some stuff"

# Did we reach the "next time" ... if yes
if ((SECONDS>=start)); then
echo "special command"
# And now set the new "next time"
((start=SECONDS+300))
fi
done


Related Topics



Leave a reply



Submit