PDA

View Full Version : SLES 11 SP3 Creating Cron Job to delete log files every 1 hour



mmoutaweh
23-Jun-2016, 07:07
Can any one help me how to create a cron job to permanently delete log files every 60 min?

I have done the below to schedule a cron job that runs daily:




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="10:00" to run the script.

I also restarted the cron by using "rccron restart".




How can we set this to work on hourly basis and how to make this start running directly as I was told that the above will need around two days to start executing?


I am still new user for SUSE and Linux, please advise.

Kind Regards,
Marwa

jmozdzen
23-Jun-2016, 09:50
Hi Marwa,

> How can we set this to work on hourly basis

as the name implies, scripts in /etc/cron.daily are run once a day. The scheduling is done by a helper script (/usr/lib/cron/run-crons), which is intended as a utility to schedule scripts more easily if run once per {hourly,daily,weekly,monthly} - see "ls -ld /etc/cron.*".

Hence you should move your script to /etc/cron.hourly and make sure your script is executable (chmod 755 /etc/cron.hourly/yourscript).

Another hint, as you're new to Linux: /bin/sh is a compatibility mode of a shell, which limits the available features when compared to nowadays' standard shell - /bin/bash.

Regards,
J

smflood
23-Jun-2016, 10:47
On 23/06/16 07:14, mmoutaweh wrote:

> Can any one help me how to create a cron job to permanently delete log
> files every 60 min?
>
> I have done the below to schedule a cron job that runs daily:
>
> 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="10:00" to run the script.

In your original thread in the SLED Forums you wanted to run a script
daily hence my advice to create a file in /etc/cron.daily - as jmozden
has already advised for hourly you should move the file to /etc/cron.hourly.

Note also that the DAILY_TIME setting in /etc/sysconfig/cron will not
have an effect on hourly jobs.

> I also restarted the cron by using "rccron restart".
>
> How can we set this to work on hourly basis and how to make this start
> running directly as I was told that the above will need around two days
> to start executing?

You might also want to look at the process creating the log files -
perhaps there is a setting to not be so verbose when logging thus not
create such large files? Or if you don't actually want the log files to
simply not log in the first place?

Another idea could be to use logrotate to "manage" the log files. By
default logrotate is run daily via /etc/cron.daily/logrotate but you
could create an hourly version:

* copy /etc/cron.daily/logrotate to /etc/cron.hourly/logrotate (you
might want to use a different file name i.e. logrotate-hourly)
* edit /etc/cron.hourly/logrotate to reference a different configuration
file (i.e. /etc/logrotate-hourly.conf)
* copy /etc/logrotate.conf to /etc/logrotate-hourly.conf
* edit /etc/logrotate-hourly.conf to point at a different logrotate.d
directory (i.e. /etc/logrotate-hourly.d)
* create a file in /etc/logrotate-hourly.d to handle
/usr/sap/NDB/HDB00/backup/log/*.*

Just an idea but this way you could have some archives of recent log
files/entries rather than just throwing them all away every hour.

> I am still new user for SUSE and Linux, please advise.

Don't worry, we all have to start somewhere.

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

jmozdzen
27-Jun-2016, 14:00
Hi Simon,

using logrotate on an hourly basis is a good advice. As an alternative to running the complete configured sets of rotatables, it might be a good idea to just invoke that single instance rotating the desired logs, by running just that single /etc/logrotate.d/... file.

Oh, and in that case it's worth noting that despite the name "/etc/logrotate.conf", that one isn't a global config file that is also included when running a single instance. You'd need to include the required statements into the individual config file that you specify when running "logrotate -c /etc/logrotate-d/...".

/etc/logrotate.conf is a wrapper config file that's including all /etc/logrotate.d/* config files via an according "include" statement, after specifying common configuration options.

Regards,
Jens

smflood
27-Jun-2016, 14:35
On 27/06/16 14:04, jmozdzen wrote:

> using logrotate on an hourly basis is a good advice. As an alternative
> to running the complete configured sets of rotatables, it might be a
> good idea to just invoke that single instance rotating the desired logs,
> by running just that single /etc/logrotate.d/... file.
>
> Oh, and in that case it's worth noting that despite the name
> "/etc/logrotate.conf", that one isn't a global config file that is also
> included when running a single instance. You'd need to include the
> required statements into the individual config file that you specify
> when running "logrotate -c /etc/logrotate-d/...".

Not exactly - /etc/logrotate.conf is specifically used by
/etc/cron.daily/logrotate hence my suggestion to create a copy then
reference the copy in a new logrotate script in /etc/cron.hourly.

Running /usr/sbin/logrotate on it's own will do nothing - it doesn't
read /etc/logrotate.conf.

Note also that there is no -c option for logrotate - to specify a .conf
file you specify it as a parameter (without any option) as per the
following line in /etc/cron.daily/logrotate:

--begin--
/usr/sbin/logrotate /etc/logrotate.conf 2>&1 | tee $TMPF
---end---

> /etc/logrotate.conf is a wrapper config file that's including all
> /etc/logrotate.d/* config files via an according "include" statement,
> after specifying common configuration options.

Hence I also suggested having the hourly logrotate.conf file (i.e.
logrotate-hourly.conf) pointing at /etc/logrotate-hourly.d/

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

jmozdzen
27-Jun-2016, 15:07
Hi Simon,

yes, sorry about that wrong "-c"... From my experience, it is a common misunderstanding that /etc/logrotate.conf would *also* be read when specifying a single config file from /etc/logrotate.d to the logrotate command. Typically, that results in different compression methods used, because the default is overridden in /etc/logrotate.conf, which in turn confused the admins I talked to about this issue :)

Regards,
J

smflood
27-Jun-2016, 15:59
On 27/06/16 15:14, jmozdzen wrote:

> yes, sorry about that wrong "-c"... From my experience, it is a common
> misunderstanding that /etc/logrotate.conf would *also* be read when
> specifying a single config file from /etc/logrotate.d to the logrotate
> command. Typically, that results in different compression methods used,
> because the default is overridden in /etc/logrotate.conf, which in turn
> confused the admins I talked to about this issue :)

I guess the confusion stems from which logrotate is being used:

* /usr/sbin/logrotate is the binary and does not read any configuration file
* /etc/cron.daily/logrotate calls /usr/sbin/logrotate with
/etc/logrotate.conf configuration file

The latter processes files in /etc/logrotate.d which can then overwrite
settings already made in /etc/logrotate.conf.

Note I write the above to hopefully help anyone else who comes along
later and finds this thread.

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