PDA

View Full Version : CRON JOB to be scheduled daily to remove log files



mmoutaweh
21-Jun-2016, 11:41
Can Any one help me on how to create a script in SUSE Linux to delete files in specific folder and then schedule it using cron?

I am newbie in SUSE and Linux world.

Thank you

smflood
21-Jun-2016, 13:15
On 21/06/16 11:44, mmoutaweh wrote:

> Can Any one help me on how to create a script in SUSE Linux to delete
> files in specific folder and then schedule it using cron?
>
> I am newbie in SUSE and Linux world.

Since you've posted in a forum for SUSE Linux Enterprise Desktop (SLED)
can I ask if you are using SLED or perhaps SLES?

With SLEx the simplest option would be drop a file in /etc/cron.daily/
which contains:

--begin--
#!/bin/sh

rm /path/to/files/filename.ext

exit 0
---end---

Obviously you want to make sure that this file is owned by the root user
and group and no other users have write permission.

This file will then be run along with the other daily jobs.

If you want to ensure these jobs always happen at a certain time each
day (rather than tied to time machine last restarted) you can set
DAILY_TIME in /etc/sysconfig/cron.

HTH.
--
Simon
SUSE Knowledge Partner

------------------------------------------------------------------------
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below. Thanks.
------------------------------------------------------------------------

mmoutaweh
22-Jun-2016, 09:38
I am using SLES (Server version).

I created the following file in /etc/cron.daily/

--begin--
#!/bin/sh

rm /usr/sap/NDB/HDB00/backup/log/*.*

exit 0
---end---

and set DAILY_TIME="12:30" to run the script at 12:30 pm but nothing happened

So I added the following command in crontab

40 12 * * * root rm -f /var/spool/cron/lastrun/cron.daily

to run it at 12:40 pm but also nothing happened.

Is there anything missing in my steps?

Thanks,
Marwa

smflood
22-Jun-2016, 15:31
On 22/06/16 09:44, mmoutaweh wrote:

> I am using SLES (Server version).

In which you should have posted to the SLES Forums @
https://forums.suse.com/forumdisplay.php?8-SUSE-Linux-Enterprise-Server
but we're here now and it could be relevant for SLED users so ...

> I created the following file in /etc/cron.daily/
>
> --begin--
> #!/bin/sh
>
> rm /usr/sap/NDB/HDB00/backup/log/*.*
>
> exit 0
> ---end---
>
> and set DAILY_TIME="12:30" to run the script at 12:30 pm but nothing
> happened

I think you need to restart cron for the change of DAILY_TIME to take
effect - since I don't know the SLES version "rccron restart" should work.

If I recall correctly it takes 2 days for a new daily job to first run -
see TID 3842311[1].

> So I added the following command in crontab
>
> 40 12 * * * root rm -f /var/spool/cron/lastrun/cron.daily
>
> to run it at 12:40 pm but also nothing happened.

Just as well. Why set cron to delete the
/var/spool/cron/lastrun/cron.daily file at 12:40 every day?

You could have added "> /path/to/logfile 2>&1" to log output to
/path/to/logfile.

> Is there anything missing in my steps?

I don't think so, you just need to be patient (and remove that cron
command from the crontab file).

HTH.

[1] https://www.suse.com/support/kb/doc?id=3842311
--
Simon
SUSE Knowledge Partner

------------------------------------------------------------------------
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below. Thanks.
------------------------------------------------------------------------

mmoutaweh
23-Jun-2016, 06:51
Thank you Simon.
I have done what you suggested and will be waiting. However, I need to do that periodically after 1 hour due to the large size of logs that is increasing quickly. I will re-post this on the SLES forum.

Thanks for your help.

Regards,
Marwa Moutaweh