3

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

  • sorry, english is not my first language. could you rephrase or explain me what a stray back-tick is? thanks – sciakysystem Apr 8 '15 at 16:20
  • oh ok.. nope that's not the error, that prlly was a typo in reporting the code here but the original script is safe. thanks – sciakysystem Apr 8 '15 at 16:23
  • change owner of directories and files using chown command – Faizan Akram Dar Apr 8 '15 at 16:44
  • is 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 – sciakysystem Apr 8 '15 at 16:47
  • 1
    No, 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. – terdon Apr 8 '15 at 17:07
2

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
| improve this answer | |
  • i 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 – sciakysystem Apr 9 '15 at 16:22
  • @sciakysystem OK, but note that you can also have issues when copying. That won't save you. If the other command is writing while you copy the file, you'll have the same problem. – terdon Apr 9 '15 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.