Solved

Cronjob not running

Posted on 2009-05-18
15
600 Views
Last Modified: 2013-12-27

 Newbie to Unix (Solaris).

I wanted to remove all the files existing in a directory every 5 mnutes.
The files that i need to remove are log files and every minute it generates 3/4 files.

I set up a cronjob for this activity but the files are not getting deleted for some reason.

Cron job used :
*/5 * * * * rm -rf /opt/app/addop/newsystem/ars/bmc/apps/service/debug/*.dbg
Also used following cronjob (for solaris) but in vain
0,5,10,15,20,25,30,35,40,45,50,55 * * * * rm -rf /opt/app/addop/newsystem/ars/bmc/apps/service/debug/*.dbg

The rm command when it is executed diretly works fine.
rm -rf /opt/app/addop/newsystem/ars/bmc/apps/service/debug/*.dbg

Please help
0
Comment
Question by:tks_g100
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 29

Expert Comment

by:Michael W
ID: 24413938
For cron, you have to state where the command actually resides -- i.e. '/bin/rm'
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24413967
execute

which rm

then get the path add before the rm command.

Also I don't think the syntax :
*/5 is valid for solaris try the second version instead..

Cheers,
K.
0
 
LVL 29

Accepted Solution

by:
Michael W earned 500 total points
ID: 24414005
Sometimes variables in command line actions aren't copied correctly in cron. It might be better to create a script (i.e. remove_files.sh) then use cron to call the executable script instead.

*/5 * * * * /path/to/remove_files.sh


#!/bin/sh

#

# Remove files from a certain directory (remove_files.sh)

#

DIRECTORY="/opt/app/addop/newsystem/ars/bmc/apps/service/debug/"

rm_cmd='/bin/rm -rf'
 

$rm_cmd $DIRECTORY/*.dbg

Open in new window

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24414371
first form is not supported in solaris:

*/5 * * * * rm -rf /opt/app/addop/newsystem/ars/bmc/apps/service/debug/*.dbg

in 2nd form , use full path name to rm

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/rm -rf /opt/app/addop/newsystem/ars/bmc/apps/service/debug/*.dbg
0
 
LVL 22

Expert Comment

by:blu
ID: 24414704
I think you already said that you did not use the */5 on Solaris, did you not? So that isn't the problem.

Nor is the problem having to give the full path to rm.  The PATH set up in cron is minimal, but not non-existent, and
rm is on the default path cron uses, so that isn't the problem.

Are you running the job as a regular user or as root? If you are running as a regular user, perhaps there is a
permissions problem. When you said it worked outside of cron, are you sure you were running as the same
user? If you are running the job as root, then is the directory NFS mounted? That might prevent the rm from
working.

Have you checked the local mail for the user you are running the job for? If cron encounters an error, it wil
send the output of the command to the local mail file. Try running just "mail" as they user.

One other thing. If you are running the cron job as root, consider not doing so. I presume that there are files
being created in the directory by others, otherwise why delete them? If it is at all possible for a random
user to create a file in that directory that houses the debug files, (i.e. world writable) then you need to rethink
your script.  Consider what would happen if I were able to create a file with the following name:

" $SHELL f.dbg"

in the directory. The glob you have would execute the command
rm -rf //opt/app/addop/newsystem/ars/bmc/apps/service/debug/  /usr/bin/sh f.dbg

That woould be bad, I think. And there are even worse possibilities.
0
 
LVL 22

Expert Comment

by:blu
ID: 24415266
I don't get it. What was the solution? There weren't any variables in the original script to be copied, so how
would putting it into a script help? If the PATH isn't set to the default, that would be a bug in cron. So what happened?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24415579
I agree with blu.  Assuming the initial crontab entry was correctly defined and running as the correct user and cron was running, then it should have needed no changes.

Something else was the problem and certainly didn't need a separate script to be written (although in general, this is not a bad approach)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 40

Expert Comment

by:omarfarid
ID: 24415822
If your platform is Sun solaris, then the accepted solution will not run since */5 * * * *  format is not supported.

I think author is requested to explain why that solution was accepted.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24415929
The representation "0/5" would not run in solaris Crontab as I told earlier in the tread. Also I really can't understand what does this script in accepted solution do that the command in the inital post does not. I am not sure if there's a bad intention in that but it is obvious that the question and the answer would not help anyone later searching the tread. What about opne a question in expert area for the evaluation of sys admins?
 
0
 
LVL 29

Expert Comment

by:Michael W
ID: 24416803
Actually in this case I agree with the other EE experts that I didn't give a 'full' solution in this case. I just supplied a partial solution via using a script rather than placing the actual rm/delete statement within the crontab entry. When referring back to the question, the 'asker' stated they were a 'newbie' when using Solaris... so it is my guess that the asker wanted something with detail (i.e. an example script) thus something a bit more concrete to work from rather than a full, drawn out explanation on why his/her original question wasn't working.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24416879
Yeah but what could have he possibly learnt from your script while the content is not new or concrete? Furthermore it is even not possible to get it worked with "0/5" thing :)

We're all here for some time now. We've all seen an asker gave credit to someone contributed lesser then the others. But in this case there's nothing to contribute. Even it does not work as you suggested. This is what we're having difficulty to understand.
0
 
LVL 29

Expert Comment

by:Michael W
ID: 24417094
@KeremE: Do an attention request for the question. Maybe a zone advisor can get in touch with the asker or reassign the points, etc. Doesn't hurt my feelings any. ;)
0
 

Author Comment

by:tks_g100
ID: 24433320

Hi all,

 I regret for my late posting here. I was tied up with my release activities which is due today and hence could not get time to reply to my post.

As i mentioned earlier in my question i tried both the commands and both were not working and i was in hurry to make this cron running to delete the growing log files in that path.

 The accepted solution (through script) worked perfectly fine for me (i used /5 only).
I am not sure really how it worked even by giving /5 to the solaris machine.

Before trying the script i also tried adding the location rm command even then it din't worked.
I lost my control of experimenting things with the command and i finally tried that accepted solution (through script) and worked fine for me.

 I really don't know how this script worked but not my command. Searching for the same answer. If anyone requires any additional information which can unfold the mystery i am here to give.

Once again thanks for the valuable information and discussion with this post and i regret the inconvenience caused for my delayed response.


Note: My response may get delayed as i am working from India.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24433370
thank you, can you do

uname -a
crontab -l

and post output
0
 

Author Comment

by:tks_g100
ID: 24442529
uname -a
SunOS servername 5.10 Generic_127127-11 sun4u sparc SUNW,Sun-Fire-15000
 
crontab -l
1 0 * * * /usr/sbin/logadm
2 0 * * * /usr/sbin/svcadm restart system-log
15 3 * * 0 /usr/lib/fs/nfs/nfsfind
*/5 * * * * /home/remove_files.sh


 
 
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now