user6742604
user6742604

Reputation:

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

I have this cronjob, that is not working for some reason, but I don't entirely understand what it does, could someone explain?

flock -n /var/www/vhosts/SOMEDOMAIN/scripts/blia_bab_import.sh.lock /bin/bash /var/www/vhosts/SOMEDOMAIN/scripts/blia_bab_import.sh

I googled that flock is supposed to lock the file and only run if it can lock the file, so that the job cannot run multiple times at the same time.

What does the -n do?

flock -n

Why is this a .lock file?

/var/www/vhosts/SOMEDOMAIN/scripts/blia_bab_import.sh.lock

I have no idea what the /bin/bash is supposed to do?

/bin/bash

Is this the script I want to execute? Why is it added as a .lock first?

/var/www/vhosts/SOMEDOMAIN/scripts/blia_bab_import.sh

Upvotes: 1

Views: 1275

Answers (1)

Nic3500
Nic3500

Reputation: 8621

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/SOMEDOMAIN/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/SOMEDOMAIN/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/SOMEDOMAIN/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.

Upvotes: 4

Related Questions