BASH - how do i detect the file date time is changed so do a copy or move now once its done? - bash

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/

Related

Read .env variables in crontab

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

deleteDirectory not working as Cron job

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 ?

How to run bash script from cron passing it greater argument every 15 minutes?

I have a simple script that I need to run every 15 minutes everyday (until I get to the last record in my database) giving it greater argument. I know how to do this with the constant argument - example:
*/15 * * * * ./my_awesome_script 1
But I need this, let's say, we start from 8:00 AM:
at 8:00 it should run ./my_awesome_script 1
at 8:15 it should run ./my_awesome_script 2
at 8:30 it should run ./my_awesome_script 3
at 8:45 it should run ./my_awesome_script 4
at 9:00 it should run ./my_awesome_script 5
...
How to make something like this?
I came up with temporary solution:
#!/bin/bash
start=$1
stop=$2
for i in `seq $start $stop`
do
./my_awesome_script $i
sleep 900
done
Writing a wrapper script is pretty much necessary (for sanity's sake). The script can record in a file the previous value of the number and increment it and record the new value ready for next time. Then you don't need the loop. How are you going to tell when you've reached the end of the data in the database? You need to know about how you want to handle that, too.
New cron entry:
*/15 * * * * ./wrap_my_awesome_script
And wrap_my_awesome_script might be:
crondir="$HOME/cron"
counter="$crondir/my_awesome_script.counter"
[ -d "$crondir" ] || mkdir -p "$crondir"
[ -s "$counter" ] || echo 0 > "$counter"
count=$(<"$counter")
((count++))
echo "$count" > $counter
"$HOME/bin/my_awesome_script" "$count"
I'm not sure why you use ./my_awesome_script; it likely means your script is in your $HOME directory. I'd keep it in $HOME/bin and use that name in the wrapper script — as shown.
Note the general insistence on putting material in some sub-directory of $HOME rather than directly in $HOME. Keeping your home directory uncluttered is generally a good idea. You can place the files and programs where you like, of course, but I recommend being as organized as possible. If you aren't organized then, in a few years time, you'll wish you had been.

Editing crontab using bash script

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.

How can I run the kippo honeypot on rasbperry pi using crontab?

I've tried several different implementations as follows:
05 11 * * * cd /home/pi/kippo/; ./start.sh
05 11 * * * /home/pi/kippo/start.sh
05 11 * * * bash /home/pi/kippo/start.sh
05 11 * * * sh /home/pi/kippo/; ./start.sh
I've tried this crontab entry and in order to test if my timezone is correct. I've also tried creating text files to ensure that my crontab is functioning well.
05 11 * * * touch /home/pi/bin/hellotestkippo.txt
I've created this text file to ensure and test if at the same time I'm trying to run Kippo can be created to serve as a checker that cron does it's job.
The problem is the text file was created but kippo didn't start at all.
Please help me on this matter. Thanks.
try this commond:
sh /home/pi/kippo/start.sh
Edit your /etc/rc.local and add:
su - pi -c kippo/start.sh

Resources