I am getting error when executing flock command - linux

when I am trying to execute flock through cron job I am getting error.
I am executing
* * * * * /usr/bin/flock -n /usr/local/monitor/asdp_cloudwatch/run_asdp0101.sh
and I am getting below error
/usr/bin/flock: bad number: /usr/local/monitor/asdp_cloudwatch/run_asdp0101.sh
Can anyone solve this. Help can be appreciated.

flock needs a lock file and a command to run. You've only specified one argument. I'm assuming that it is the command, so must also specify the command to run. Something like that:
* * * * * /usr/bin/flock -n /path/to/lockfile /usr/local/monitor/asdp_cloudwatch/run_asdp0101.sh
You need to adjust /path/to/lockfile of course.

Related

Crontab cannot execute

I tried to use crontab to execute my py file everyday, but it can only create empty log file
0 8 * * * /usr/local/bin/python3 /Users/UserName/Downloads/Crawling_1.py > /Users/UserName/Downloads/log.log
Then I tried to use SHELL file to execute a simple demand, if I put log file settings inside .sh file, no log file was created. Similarly, the crontab did not execute when I put python3 demand inside the SHELL file.
echo 1 > /Users/UserName/Downloads/new_log.log > /Users/UserName/Downloads/log.log
But if I directly run echo in Crontab, it can work out perfectly.
* * * * * echo 1 > /Users/UserName/Downloads/new_log.log
Does anyone know why this is happening? Thank you so much.
Try it with >>:
0 8 * * * /usr/local/bin/python3 /Users/UserName/Downloads/Crawling_1.py >> /Users/UserName/Downloads/log.log
then with >> it will create a file if it doesn't exist. If the file existe, it will be appended to the end of the file.
With > the whole file will replace it, if the file exist. If the file not exist*, nothing happens.
You can do the command also with >, but be sure, that the .log-file, where you will write inside, exist!

Searching for end removing crontab jobs

I want to check to see if the line exists with a script:
*/2 * * * * ls -1 /var/www/html/shared-media > /var/www/html/files.txt
I also need a way to remove that SPECIFIC line in the same script.

Files in Cron not running

I added 2 files to my crontab and I am trying to make them run every minute but can not figure out what is wrong. I tried to follow steps online and thought I did everything the same as in the tutorial.
1 * * * * /Desktop/wget/fb_BQ_GCS2.py
1 * * * * /Desktop/wget/postslack3.py
~
1 * * * * /Desktop/fb_BQ_GCS2.py
1 * * * * /Desktop/postslack3.py
~
I have tried both of these and still get nothing. The only thing I can think of is that maybe I have an error in my path and in can not find the files? First time using Cron so thanks for the help!
I figured out a solution to my question. In order to run a python script through cron you need to add Python to the beginning of the command. See code below for examples.
*/1 * * * * python ~/Desktop/fb_BQ_GCS2.py
*/1 * * * * python ~/Desktop/postslack3.py

Crontab schedule issue

I have several scripts that are run every 3 minutes and schedule looks like this:
*/3 * * * * /some/script1.php
*/3 * * * * /some/script2.php
*/3 * * * * /some/script3.php
I suppose that these scripts run at the same time, but I wish that these scripts run every 3 minute but not in the same time. Tell me please how can I reach this.
You can't reach that with the lines you are having, those will allways be running at the same times. However, you can simply create one "master" script that gets called via cron and then calls the scripts one after the other.
*/3 * * * * /usr/bin/php /some/masterscript.php
masterscript.php:
<?php
exec('/usr/bin/php /some/script1.php');
exec('/usr/bin/php /some/script2.php');
exec('/usr/bin/php /some/script3.php');
?>
EDIT:
Depending on your server's setup - install node.js. There's a cron package you can set for every second. Maybe this can help..
You can but not with that syntax, instead you should use this kind of syntax:
1,4,7,10,13,16,etc... * * * * /some/script1.php
2,5,8,11,14,17,etc... * * * * /some/script2.php
and so on....
If you just want each script to execute in turn, with the second not starting until the first has finished, and so forth, just put them all in a single cron command.
cron invokes each command by passing the command string to /bin/sh -- and the shell can very easily invoke several commands in sequence.
*/3 * * * * /some/script1.php ; /some/script2.php ; /some/script3.php
do you think a couple of seconds between scripts run could be enough ?
what about a command like this ?
*/3 * * * * echo "<?php echo 'Start ...';sleep(2);echo Go; ?>"|php /some/script1.php
*/3 * * * * echo "<?php echo 'Start ...';sleep(4);echo Go; ?>"|php /some/script2.php
*/3 * * * * echo "<?php echo 'Start ...';sleep(6);echo Go; ?>"|php /some/script3.php
You could also substitute fixed waiting time with random waiting time.
Instead of sleep(2) try a generic sleep(rand(1,10)).
I hope this could be useful

Multiple Query String Items in crontab Job

I have the following cron job command:
* * * * * /usr/bin/lynx -term=vt100 http://abc.com/dir1/di2/script.php?action=add&config=xyz >/dev/null 2>&1
My PHP script does not recognize _GET['config'] and I get a "Cron Daemon" email message which seems to alert me that the crontab instruction is not correct.
If I take out the 2nd _GET var I do not get the "Cron Daemon" email.
Any thoughts or suggestions on how to define multiple query string items in a crontab job?
BTW, I tried the URL Encode char for the ampersand and that did not work either.
Try putting your url in quotes :
* * * * * /usr/bin/lynx -term=vt100 "http://abc.com/dir1/di2/script.php?action=add&config=xyz" >/dev/null 2>&1
For the little explanation, & is a special character which put the process in the background, so you have to put the url in quotes, otherwise cron try to put the first part in the background and execute the second part.

Resources