Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Zip with crontab

Status
Not open for further replies.

zaradell

Technical User
Dec 21, 2007
77
0
0
PT
Good afternoon to you all

I really need help...

I´m having a problem using ZIP with crontab. For some reason the cronjob it not being made as specified in crontab.When i run the command in CLI it works fine. The problem is just when i try to run it in crontab:

30 12 20 * * root /usr/bin/zip -r /Copy/$(date +%Y_%m_%d).zip /Backups_Tools_Automatic/Nagios/*


I´ve tried creating a script .sh and run it with crontab but no luck. I´ve tried to remove "root" from the begining of the line, but nothing. I´ve also tried to replace $(date +%Y_%m_%d).zip with name.zip but also nothing.

Is there anything specific I have to do, so that ZIP works with Crontab?

I´d really apreciate some help.

Thkx
 
os?
do other things work in cron?
are you getting any mail with the output?
you might need to redirect both stdout and stderr to see what the problem is.
 
OS - Fedora Core 4

I have other things, such as copies (cp) and removes (rm) and they work ok.

As for mail, I´m not getting any. There´s no messages in /var/logs/cron and /var/logs/messages.

It´s like the command line is not even there.

Redirect both stdout and stderr? I don´t know what they are: I´m kinda of a newbie in linux.

Thkx for your reply stanhubble.
 
redirection: > = initialize and redirect
>> = append and redirect
1 = stdout
2 = stderr

what is the output of crontab -l
if nothing and you are using /etc/crontab


30 12 20 * * root /usr/bin/zip -r /Copy/$(date +%Y_%m_%d).zip /Backups_Tools_Automatic/Nagios/* >> /tmp/ziplog 2>&1

you may also have to give the full path to date
 
stanhubble

I am using /etc/crontab

Is that a problem?

I can create a crontab for root with crontab -e, but I thought /etc/crontab was the system crontab. Doesn´t it have priority over all the crontabs?
 
% are special to cron. read " man 5 crontab "

The entire command portion
of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in
the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash
(\), will be changed into newline characters, and all data after the first % will be sent to the com-
mand as standard input.


So try this:

30 12 20 * * root /usr/bin/zip -r /Copy/$(date +\%Y_\%m_\%d).zip /Backups_Tools_Automatic/Nagios/*

Cron will strip the escape the \% to feed it to the shell as %. At least it worked for me.

BTW, this is pretty esoteric, and I would personally jam the whole thing in a bourne shell and avoid the issue.

 
I don´t think there´s a problem with the date path. I´ve tried to replace the date string with a name and nothing:

30 12 20 * * root /usr/bin/zip -r /Copy/name.zip /Backups_Tools_Automatic/Nagios/*

I figured it could be a problem with the size of the directory I´m trying to zip, so I´ve tried to test zip with a small txt file...no luck!

For some reason crontab screws up the zip process. I´ve tried with tar and gunzip but nothing.

I think there´s some kind of small kink in crontab with zip,gunzip,etc...I just don´t know how to fix it.

As for jamming the whole thing the shell, this is a process that has to be done 3 times a week...and it takes a butload of time doing it. I really wanted to automatize it.
 
he means put the command in a shell and execute the shell from cron so the crontab line is clean.
 
My cron does not support the user in the sixth field, which I believe is a "new" style crontab (to avoid using su)

Make sure that your crontab supports that, it should log the execution in /var/log/cron or /var/cron depending on how /etc/syslog.conf is setup.

Also, I meant to just call the thing like:

30 12 20 * * /etc/dozipit

where /etc/dozipit is:

#!/bin/sh
/usr/bin/zip -r /Copy/$(date +%Y_%m_%d).zip /Backups_Tools_Automatic/Nagios/*

In other words, take all the variables outside of cron.




 
I´ve tried it. I´ve created a script.sh and I´ve tried to run it with crontab. It didn´t work.

I was able to run the script from CLI, but Crontab didn´t run it.
 
Exactly StanHubble.

BTW, you can avoid the "*" expansion by not using it:

Instead of say:

/usr/bin/zip -r /Copy/$(date +%Y_%m_%d).zip /Backups_Tools_Automatic/Nagios/*

use

/usr/bin/zip -r /Copy/$(date +%Y_%m_%d).zip /Backups_Tools_Automatic/Nagios/

Since the -r is recursize, it should give you the same results without having to expand the '*', which gets into trouble with a line length problem. Developers implementing with a recursive option usually write use libraries that are limited by directory size.


 
doh!! i missed the %'s .... yes this will cause a problem in crontab.

/etc/crontab is the (linux) "system" crontab and supports the userid's.
as far as i know there is no special priority assigned to it. you can (re)nice any individual process from it to give it more/less priority.
 

Yeah, I see it /etc/crontab, but it isn't mentioned (at least in RHEL 5) in "man 5 crontab"

I just tried it (in the normal crontab:

* * * * * root /bin/date >> /tmp/date.log

and it tries to execute or find a script called "root" because /tmp/date.log gets created but has no value.

 
Ok...so, before u all get wayyy oftopic :p

I´ve tried /usr/bin/zip -r /Copy/$(date +%Y_%m_%d).zip /Backups_Tools_Automatic/Nagios/

without the *

...no results.

I´ve created an sh script.

...no results.

Any other sugestions?
 
stanhubble

I cannot answer you that right now, seeing as I´m at home now, but I will see it tomorrow at work.

One thing I can tell you, is that I gave zip 777 permissions. So anybody could supposedly run it, right?
 
no /etc/crontab should only be 644 root:root


also i just tried the following:

*/5 * * * * root /usr/bin/zip -rq /tmp/$(date +\%Y_\%m_\%d) /var/run/*.pid

and it worked fine.
 
Is this through crontab/cron or command line?

Here is my test, works fine on command line

[root log]# /usr/bin/zip -r /tmp/zipfile.$(date +%Y%m%d).zip /tmp/zipit
adding: tmp/zipit/ (stored 0%)
adding: tmp/zipit/passwd.zip (stored 0%)
adding: tmp/zipit/a/ (stored 0%)
adding: tmp/zipit/a/group (deflated 48%)
adding: tmp/zipit/passwd (deflated 62%)
[root log]# /usr/bin/zip -r /tmp/zipfile.$(date +%Y%m%d).zip /tmp/zipit/*
updating: tmp/zipit/passwd.zip (stored 0%)
updating: tmp/zipit/a/ (stored 0%)
updating: tmp/zipit/a/group (deflated 48%)
updating: tmp/zipit/passwd (deflated 62%)
[root log]#

Both create the same correct output file.

Did you take the root field out of your crontab?




 
stanhubble

I´ll try it tomorrow.Hopefully it will work.

Btw, what´s your OS? You´re crontab version is probly more recent than mine.
 
Btw stanhubble

In your crontab line

*/5 * * * * root /usr/bin/zip -rq /tmp/$(date +\%Y_\%m_\%d) /var/run/*.pid

you didn´t specify the zip extension in the output file:
/tmp/$(date +\%Y_\%m_\%d)

In which format did the output file came out as?

Maybe the problem is in specifying the output file as a *.zip file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top