Logrotate script only triggers correctly during manual execution - linux

Good day!
I tried to use the logrotate facility of linux to rotate my tomcat logs. I created a script named tomcat under /etc/logrotate.d
/path/to/tomcat/catalina.out {
missingok
copytruncate
daily
rotate 10
compress
size 1M
}
Logrotate seems to be triggered daily via cron.daily but my tomcat logs are not rotated. I tried to execute the script manually by entering this command:
/usr/sbin/logrotate /etc/logrotate.conf
Surprisingly, it worked. Do you have any idea why cron job cannot rotate it? Below is the setup in crontab:
0 7 * * * root run-parts /etc/cron.daily
Thanks!

It seems like you need to add the following to /etc/cron.daily/logrotate:
/usr/sbin/logrotate /etc/logrotate.conf
Try running logrotate to verify:
cd /etc/cron.daily/ && ./logrotate

Related

logrotate: daily rotation didn't work even though i used -f option

Here's the logrotate config file under /etc/logrotate.d/mylog
/var/www/www.mysite.com/logs/* {
daily
rotate 365
compress
delaycompress
missingok
notifempty
create 0664 apache apache
}
I set this yesterday at 4 pm, and today at 10am i didn't see any compressed file.
I tried logrotate with -d and -f option
With -d option, i got this output
reading config file mysite
reading config info for /var/www/www.mysite.com/logs/*
Handling 1 logs
rotating pattern: /var/www/www.mysite.com/logs/* after 1 days (365 rotations)
empty log files are not rotated, old logs are removed
considering log/var/www/www.mysite.com/logs/access-2018-08-08.log
log does not need rotating
With -f option, nothing appeared.
But i noticed it added the .1 suffix to each of my log.
Anyone please explain what is this strange behavior please?
Logrotate does not have a built-in scheduling system.
If you want to clean up logs manually once, sure you can run it manually:
logrotate /etc/logrotate.d/mylog
And if you check logrotate man page for -f behaviour:
-f, --force
Tells logrotate to force the rotation, even if it doesn't think this is necessary.
Sometimes this is useful after adding new entries to a logrotate config file,
or if old log files have been removed by hand, as the new files will be created,
and logging will continue correctly.
-d, --debug
Turns on debug mode and implies -v. In debug mode, no changes will be made to
the logs or to the logrotate state file.
So with -f option you forcefully rotate all logs thus the .1 suffix.
And with -d option it is a dry run so you only preview the changes.
Now if you want to run it daily, you have to make use of cron.
By default after installation logrotate should have a default configuration file /etc/logrotate.conf and a default cron job /etc/cron.daily/logrotate
You can either make use of them or create your own configuration file and cron job.
Say if you place your custom configuration at /etc/logrotate.d/mylog then you can place the following cron job at /etc/cron.d/logrotate-cron to run it daily.
0 0 * * * root /usr/sbin/logrotate /etc/logrotate.d/mylog --state /etc/logrotate.status
You can check the file /etc/logrotate.status to make sure it ran.

how to remove logs in every 2 hours in Linux Terminal

SETUP CRON To Delete File
how to remove logs in every 2 hours in Linux Terminal
below is my path where my log file is kept
/home/test/dev/sample.log
I want to set a cron so every 2 hours sample.log file will be delete.
can you please help me to set that cron in Linux terminal
0 */2 * * * <username> echo "" > /home/test/dev/sample.log
add in /etc/crontab, restart crontab service.
Or maybe it's better to use logrotate.

Run cron job hourly

I use let's encrypt for getting certificates and I want to setup renewal for certificates.
So, I decided to check if cron works fine.
I created three file in daily.hourly folder:
test-h:
/sbin/ifconfig >/home/bitnami/ipt
test-h2:
#!/bin/bash
/sbin/ifconfig > /home/bitnami/ipt2
test-h3.sh:
#!/bin/bash
/sbin/ifconfig >/home/bitnami/ipt3
But, I don't see my files in home directory. How to properly use cron.daily?
PS. The cron servive is started, I checked.
I restarted it also just to make sure that changes is applied.
The crontab file contains record for cron.hourly:
17 * * * * root cd / && run-parts --report /etc/cron.hourly
I am not linux guy, so, if it possible get me detailed answer please.
The problem is you didn't chmod +x your scripts. That's needed to make them executable.

How to use cron.d file when using Ubuntu

I have a backup script that I can run successfully manually as root
backup perform --trigger confluence --config-file /etc/backup/config.rb --log-path=/var/log > /dev/null
In the /etc/cron.d folder I have file confluence_backup with the following contents
# Crontab for confluence_backup managed by Chef. Changes will be overwritten.
0 0 * * * root backup perform --trigger confluence --config-file /etc/backup/config.rb --log-path=/var/log > /dev/null
For some reason this backup script will not run. I don't see messages in /var/log/backup or in syslog for that matter. I tried restart of cron service but still the job does not run.
Why is the script not running?
First, have you confirmed that the executable 'backup' is in the root user's PATH variable?

running a script in crontab

I have a very simple script in my crontab that I want to run every day. It is located in /home:
-rwxr-xr-x 1 root root 40 Apr 15 08:01 kill_slony_stop_sql.sh
It has execute permission and here is the content:
#!/bin/bash
slon_kill;rcpostgresql stop
and here is the cron line for it to run daily:
56 12 * * * /home/kill_slony_stop_sql.sh
But it is not working for some reason. When I type /home/kill_slony_stop_sql.sh in the command line, it works good but it is not working in the crontab.
Any thoughts?
It is most likely a PATH issue. Have a look at Why is my crontab not running and be sure to set a PATH so that it can call your slon_kill command.
Also, add some debug to your cron
56 12 * * * /home/kill_slony_stop_sql.sh &>/tmp/errorcron.log
And also look at the logs; cron logs its actions via syslog, which (depending on your setup) often go to /var/log/cron or /var/log/syslog.
I had the same problem with a daily cron job, I used the #daily but this will run at 00:00 every day.
#daily /usr/local/bin/msa70_check.sh
was the cron tab line i added, below is the script i run.
#!/bin/bash
# msa70 disk check
/sbin/mdadm --detail /dev/md0 /dev/md1|
/bin/mailx -s"Disk check on server123 please check" person#domain.com
I also had to edit my script and add /sbin/ and /bin in front of mdadm and mailx for the cron job to run

Resources