Creating Cron Job to delete log files every 1 hour

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

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

On 23/06/16 07:14, mmoutaweh wrote:
[color=blue]

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.[/color]

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.
[color=blue]

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?[/color]

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.
[color=blue]

I am still new user for SUSE and Linux, please advise.[/color]

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.

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

On 27/06/16 14:04, jmozdzen wrote:
[color=blue]

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/…”.[/color]

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—
[color=blue]

/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.[/color]

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.

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 :slight_smile:

Regards,
J

On 27/06/16 15:14, jmozdzen wrote:
[color=blue]

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 :)[/color]

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.