I have a crontab and .env file. And I want to reach .env variables from crontab. Is it possible?
.env
variable1=value1
variable2=value2
crontab
2 11 * * * curl -Ssi -X POST -u 'value1:value2' https://example.com...
I think the best way is to create a script that reads you .env file an runs the command, like this:
#!/bash
# This is /my_path/my_script.sh file,
# do not forget to set executable permission by chmod
# Reading vars
. /my_path/my_env.env
# Calling the command
curl -Ssi -X POST -u "${variable1}:${variable2}" https://example.com...
and your crontab line will be like this:
2 11 * * * /my_path/my_script.sh
or alternatively in not so readable manner, directly in the crontab:
2 11 * * * . /my_path/my_env.env; curl -Ssi -X POST -u "${variable1}:${variable2}" https://example.com...
Source your .env file within cron, and I assume you mean $variable1:$variable2.
2 11 * * * . /path/to/.env; curl -Ssi -X POST -u "$variable1:$variable2" https://example.com...
Related
How to set codeigniter url in crontab
I have tried with
* * * * * php-cli /var/www/html/msp/index.php partner cron_test
But it did not work
My application url is
localhost/msp/index.php/partner/cron_test
My php code
public function cron_test()
{
if($this->input->is_cli_request())
{
$fo= fopen("/var/www/cron_test".microtime().".txt", "w") or die("Unable to open file!");;
fwrite($fo,"Cron tab test");
fclose($fo);
}
else
{
echo "You dont have access";
}
}
Project located under /var/www/html/
Set correct php installation path or directly use php in command to execute your cron, if you want to find out your installation path try using command.
which php
It will give you installation path like used in below crontab command
* * * * * /usr/bin/php /var/www/html/msp/index.php partner cron_test
If you want to debug your cron url, you can directly run /usr/bin/php /var/www/html/msp/index.php partner cron_test command from Ubuntu terminal. It will show you the direct output So you can debug wheather it's working or not then set in crontab accordingly.
I have a job which I want cron to run at given interval , this removes some old entries from my db and then removes the files that were uploaded with the db entry.
If I run the task from the terminal it works fine, (removing both db entries and the files uploaded). But when I leave the task to cron, it does remove the entries in the db , but it doesn’t remove the uploaded folders in my public directory.
the code that removes the files looks like this
$machtiging = File::files('icec/'.$icecconsult->accesstoken.' /machtiging');
if(count($machtiging) > 0){
File::deleteDirectory(public_path() . '/icec/'.$icecconsult->accesstoken);
}
so it looks if there there, if so , delete them , but this just doesn’t work , ive tried putting the cron job run as root , www-data , and my user , all with same result . files and folder permission I have set them to 777 to be sure, but this doesn’t seem to be the problem.
Ive also tried adding shell=/bin/bash but that didnt do the trick either
Any help on solving this issue would be much appreciated
Update
the crobline looks like this
* * * * * /bin/bash /home/ice/verlopenicec.sh >> /tmp/output 2&>1
also tried
* * * * * /home/ice/verlopenicec.sh >> /tmp/output 2&>1
and
* * * * * /usr/bin/php /var/www/wemedic/artisan verwijder-verlopen-icec-consult 1>> /dev/null 2>&1
All seem to run . its just it doesnt delete or move the directory it needs to
trying to get some debug data , but nothing is showing up
the verlopenicec.sh script itself looks is just a say reference to the original script that laravel should run . thouth might be handy to make a script to test why laravel aint deleting the directory.
script looks like this
#!/bin/bash
SHELL=/bin/bash
USER=ice
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
/usr/bin/php /var/www/wemedic/artisan verwijder-verlopen-icec-consult
wich runs a laravel command that looks like this
$icecconsult = Icecconsult::where('id', '=', $consult_id)->firstOrFail();
$icecconsult->expire = Carbon::now();
$icecconsult->status = 'Gesloten';
$icecconsult->save();
$icec = Icec::where('id', '=', $icecconsult->icec_id)->firstOrFail();
$icec->delete();
$machtiging = File::files('icec/'.$icecconsult->accesstoken.'/machtiging');
if(count($machtiging) > 0){
// File::deleteDirectory(public_path() . '/icec/'.$icecconsult->accesstoken);
$move = 'mv /var/www/wemedic/public/icec/'.$icecconsult->accesstoken.' /tmp' ;
shell_exec('SHELL=/bin/bash');
shell_exec('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games');
shell_exec($move);
// File::deleteDirectory('/var/www/wemedic/public/icec/'.$icecconsult->accesstoken);
}
return;
(ive commented out the delete function and tried to move it to the temp directory , but having the same result with moving or deleting. both work If I directly run it , but not when cron runs it . Cron does run the task , cause I can see it beeing fired in the /var/log/syslog and the database entru does get changed )
Ive tryied deleting , then moving it to the temp folder, both work if I run them directly , but none work when I leave it to cron/ laravel cron scheduler
If also tried to get a response (tru/false ) from the delete function, but when I try to save that to the db to see it , the function seems to not execute.
dd($machtiging) returns an array like below, showing the files in the folder , after knowing there are files in the folder, it should go ahead and delete the complete folder allong with any files/sub directories located in it
array:1 [▼
0 => "icec/a89ce4c9010e0a745308b29782b5eeae/machtiging/machtiging.pdf"
]
Thanks for you help
Try with this crontab :
*/1 * * * * ice /bin/bash /home/ice/verlopenicec.sh >> /tmp/output.log
Change your bash script to :
#!/bin/bash
moment="`/bin/date +%y_%m_%d`"
echo "--- The script has been executed on $moment ---"
/usr/bin/php /var/www/wemedic/artisan verwijder-verlopen-icec-consult
It should work better, but if not, could you paste here the content of your generated /tmp/output.log ?
I have a list of crontab entries:
0 * * * * /home/tomcat/abc.sh
0 * * * * /home/tomcat/def.sh
I want to perform an action through a bash script and it requires one of the cron jobs to be disabled.
#0 * * * * /home/tomcat/abc.sh
0 * * * * /home/tomcat/def.sh
How can I comment a single cron job using bash script?
Please help. Thanks!
That's kinda dangerous in my book, I wouldn't recommend doing that. Instead, I'd update your script so that it creates a file (like /tmp/MY_SCRIPT_LOCK or whatever) at the start and removes the file at the end. Then just update the cron job so it doesn't run if it finds the file:
0 * * * * test -f /tmp/MY_SCRIPT_LOCK || /home/tomcat/abc.sh
If you want to add a comment (#) at a particular line you can use -
Third line:
sed '3s/^/#/' filename
You can save this as new file or use output redirections.
I have a shell script for getting data from some servers, in ~/.bin/shellScript :
OLD="$(curl --silent http://someServer:12345/stats.json | json someKey)"
NEW="$(curl --silent http://otherServer:12345/stats.json | json someKey")
echo "OLD $OLD - NEW $NEW"
I want to echo the results for running it interactively, but I've been wanting to log the results collected too.
So crontab -e, and add */5 * * * * /home/user/.bin/shellScript >> /media/dump/scriptDump.
Running the script interactively works fine - I get OLD 123 - NEW 456, but when I look at what's been running from cron, I get OLD - NEW with no data being grabbed with curl.
As discovered in the comments, you need to add the full path of json when you are calling it. This is because crontab's limited environment.
So instead of
OLD="$(curl --silent http://someServer:12345/stats.json | json someKey)"
NEW="$(curl --silent http://otherServer:12345/stats.json | json someKey")
it has to be
OLD="$(curl --silent http://someServer:12345/stats.json | /path/to/json someKey)"
NEW="$(curl --silent http://otherServer:12345/stats.json | /path/to/json someKey)"
^^^^^^^^^^^^^
[[Note your second line had ") instead of )"]]
Otherwise, you can also add the json path into crontab, as indicated on How to get CRON to call in the correct paths:
PATH=/usr/local/sbin: ... :/path/of/json
I have a static directory: /var/tmp/files
This directory is only shared with users for upload/download via SFTP, it has some static file names such as:
recording-security.frontdoor.avi
recording-security.backdoor.avi
recording-security.parkingspace.avi
....
from another PC via SFTP those files are getting removed/edited/updated/added etc
Now another path: /var/www/html/livevideo-stream/
those files are copied, moved from /var/tmp/files
How can i using BASH read those files were edited or newly added or overwritten? So, that my script can move valid contents from /var/tmp/files to livevide-stream only those which has been modified or newly added etc?
$ crontab i have:
0 7 * * * /var/tmp/finishit.sh
0 8 * * * /var/tmp/finishit.sh
0 9 * * * /var/tmp/finishit.sh
0 19 * * * /var/tmp/finishit.sh
0 20 * * * /var/tmp/finishit.sh
$ cat /var/tmp/finishit.sh
#!/bin/bash
cd /var/tmp/files
while :
do
"""
how do we now validate those files which was modified or changed or newly added and place them in that directory?
"""
# echo $1 $2
cp -R /var/tmp/files/* /var/www/html/livevideo-stream/
sleep 1
done
Your cron will launch your script several times per hour, and your script does not terminate due to the while : loop... You will end up with a lot of background scripts trying to copy stuff ever second.
You should simply replace the whole script with
rsync -vaq /var/tmp/files/* /var/www/html/livevideo-stream/