Create zip files in /var/www/html using crontabs
Joining commands with &&
means that the command on the right will only run if the one on the left was successful. This means that your crontab will fail the first time it is run since there is no zip file in /var/www/html/
so the rm /var/www/html/my-zip-file*.zip
fails and the mv
will not be executed.
So, you can either create a file of the right name that can be deleted and keep the same cron command:
touch /var/www/html/my-zip-file.zip
Or, you can use ;
instead of &&
:
cp /home/myuser/myworkingdir/file.txt /home/myuser/file.txt &&
cd /home/myuser &&
zip my-zip-file-$(date "+%b_%d_%Y_%H.%M.%S").zip file.txt &&
rm file.txt &&
rm /var/www/html/my-zip-file*.zip ;
mv my-zip-file*.zip /var/www/html && cd
You are also making this way more complex than it needs to be. The copying of /home/myuser/myworkingdir/file.txt
to /home/myuser/file.txt
is unnecessary since you're only using that to zip it and then are deleting it. The cd
commands are not needed, you can use the full path. There's also no reason to cd
at the end. All you need is one command to remove any zip files from the target directory and one command to zip them:
rm /var/www/html/my-zip-file*.zip &&
zip /var/www/htmlmy-zip-file-$(date "+%b_%d_%Y_%H.%M.%S").zip /home/myuser/myworkingdir/file.txt
Related videos on Youtube
sciakysystem
Updated on September 18, 2022Comments
-
sciakysystem over 1 year
As the title explains, I'd need to take a file and put it into a zip archive once a day; also, the zip file has to be moved in /var/www/html, where a .php scripts allow users to download it.
Assuming this:
- the absolute path of the file is
/home/myuser/working-directory/file.txt
- I put all scripts file I need to run using cronjobs in
/usr/scripts
- I programmed the following cronjob using
sudo crontab -e
, instead ofcrontab -e
because /var/www/html needs administrative privileges
The results of my thoughts are the followings:
create-zip.sh
#!/bin/bash cp /home/myuser/myworkingdir/file.txt /home/myuser/file.txt && cd /home/myuser && zip my-zip-file-$(date "+%b_%d_%Y_%H.%M.%S").zip file.txt && rm file.txt && rm /var/www/html/my-zip-file*.zip && mv my-zip-file*.zip /var/www/html && cd
sudo crontab -e
@daily sh /usr/scripts/create-zip.sh
Well.. it doesn't work. I think that the problem is something related with privileges because I get
file.txt
copied in/home/myuser
, and also the zip is created. But then i can't get the zip moved to/var/www/html
, even if the crontab is running under Root privileges.Any idea?
Also..since a .zip is created once a day, I'd need to remove the previous .zip from /var/www/html before move the new one in there. I tried using
rm /var/www/html my-zip-file-*.zip
(check the create-zip.sh above) but it doesn't work too.. so I guess it's something wrong with privileges. /var/www/html is into the group www-data and its owner too is www-data.
-
sciakysystem about 9 yearssorry, english is not my first language. could you rephrase or explain me what a stray back-tick is? thanks
-
Faizan Akram Dar about 9 yearschange owner of directories and files using chown command
-
sciakysystem about 9 yearsis there a valid motivation to do that? i mean.. that script is running under "sudo", why shouldnt it be able to write into a directory owned by www-data? sudo should be able to write everywhere afaik
-
terdon about 9 yearsNo, do not change the permissions of system dirs. You're right, the problem is elsewhere. Try adding
2>> /tmp/cron.log
after each command (before the&&
) to see if any error messages are created. That will create a file called/tmp/cron.log
. If that file is created, post its contents here. Also note that if there is no zip file in/var/www/html
, the&&
will make it stop there and the new one will not be copied. Therefore, this won't work the first time unless you create a zip file to be deleted. -
sciakysystem about 9 years@terdon.. you are right.. the problem was that the script found no .zip to remove.. so it was returning an error .. i solved using ";" instead of "&&" after the remove command.. thanks.. write this as answer
- the absolute path of the file is
-
sciakysystem about 9 yearsi need to copy them because the files i need to put into a zip are always updated/written by another program.. hence, if the .zip command happen while another program is updating the file, it may cause issues. Coping it, the main program and the zip command will work on different files. thanks