Check logfile size and alert if not updated in 30 minutes - linux

I believe this is possible to do but not being an expert on Linux and shell scripting.
I believe I can use the watcher command to check the logfile directory but I am not sure how I would set it up to send an email alert to advise that this logfile directory hasn't been updated in 30 minutes.

Perhaps crontab would be of use to you:
Perform an action every minute:
echo "* * * * * touch $(pwd)/washere1" | crontab
Perform an action every 30 minutes:
echo "30 * * * * touch $(pwd)/washere2" | crontab
Check for the existence of the file "washere1" or "washere2" to verify that the command worked.

Related

Created cron job to run every 2 mint

I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.

Trigger a bash script at startup to execute periodically

I have made a bash script, let's call it script.sh, which has the next sctructure:
#!/bin/bash
while true
do
do_something()
sleep 1800 #seconds
done
I want the script to run as a task at startup although there is no user connected to the system. I thought that I could use 'nohup script.sh' but I don't know if I can use it at startup without any user connected. Have anybody some idea?
Look into using /etc/cron.hourly/ for an hourly script. It will run hourly at some interval past the hour. On RHEL, this is defined in /etc/cron.d/0hourly as 1 minute past the hour.
You could then extend this framework for half-hour intervals (1800s = 30 minutes), e.g., in /etc/cron.d/1_halfhourly:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
00,30 * * * * root run-parts /etc/cron.halfhourly
And put your script, or symlink it, in /etc/cron.halfhourly.
Naturally, this could be extended right down to one minute intervals, e.g., in /etc/cron.d/2perminute:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
* * * * * root run-parts /etc/cron.perminute
This would run every script under /etc/cron.perminute each minute.

Cpanel CronJob several days of the week

I am new to cron jobs, and I want to run a script on several days of the week
I did create a CronJob but it didn't run last night.
I want to execute the script every night at 00:10 on Sunday through thursday
So I added this as the Job
10 00 * * 0,1,2,3,4 execute.php
Can somebody tell me what I'm doing wrong?
Set it to run daily and just add this to the top of your script
if (date('l') == 'Friday' || date('l') == 'Saturday') exit;
That way it won't even do anything unless it's a day you require and saves you a headache.
root user on shell (WHM/Cpanel over Centos)
# crontab -e #Edit cron jobs for user root
10 00 * * 0,1,2,3,4 /usr/local/cpanel/3rdparty/bin/php /full/path/to/execute.php >/dev/null 2>&1 #If not like report to email
user cpanel
with shell acces
$ crontab -e #Edit cron jobs for user
10 00 * * 0,1,2,3,4 /usr/local/cpanel/3rdparty/bin/php /full/path/to/execute.php >/dev/null 2>&1 #If not like report to email
or
10 00 * * 0,1,2,3,4 /usr/bin/php /full/path/to/execute.php >/dev/null 2>&1 #If not like report to email
without shell access
Enter Cpanel and go to Cronjobs.
Put your tiem on each text input area.
Put your job 10 00 * * 0,1,2,3,4 /usr/local/cpanel/3rdparty/bin/php /full/path/to/execute.php >/dev/null 2>&1 #If not like report to email
Explanations
/usr/local/cpanel/3rdparty/bin/php PHP compiled for WHM/Cpanel functions.
/usr/bin/php PHP used by normal Cpanel installation
It's possible other paths if your server used for example, Couldlinux with multiples PHP versions.

Linux: Crontab Job without Interfering with Sys Admin's Jobs?

I need to create a new crontab job in a Redhat Linux environment. I have sudo access to that but I don't think I can do everything on that system--some higher level sys admins, for example, disable any firewall changes I make.
So here is my crontab command:
crontab e
and that brings up a screen like:
33 2 * * * /usr/bin/cu-firewall update > /dev/null 2>&1
30 1 * * * /root/update_atbi_website > /dev/null
0 4 * * * /home/prov356/scripts/opnforumbackup
I want to not send email and I have done it successfully in my local VM:
MAILTO=""
# execute 15 minute
*/15 * * * * perl /db_xenia/pl/get_usgs.pl
Question: If I were to append the above to the existing crontab info will it prevent sending of emails to the sys admin too? I don't want to get into trouble! Perhaps, I could append /dev/null after my Perl commands?
Thanks.
Never mind: Per #Basile's comment, I didn't need to be sudo. So I logged in as non-sudo and ran crontab -e; this time there were no sys admin entries. So I simply entered my own configs, saved, and the cronjob seems to be running fine.
Thanks.

Crontab - simple echo not running

I've got such situation:
I want to schedule a job with crontab on a linux server. I'm not super-user, so I'm editing (with crontab -l, editor vim) only my crontab file. For testing, I put there:
* * * * * echo asdf
And the job is not running. Is the restart of the server needed? Or maybe some administrator move?
May be it is, cron jobs will run in their own shell. So you can't expect to see asdf on your console.
What you should try is
* * * * * echo asdf > somefile_in_your_home_directory_with_complete_path.log
Next check the file by doing a tail:
tail -f somefile_in_your_home_directory_with_complete_path.log
And if it's not, check if the cron daemon itself is running or is down:
# pgrep crond
OR
# service crond status
If you want to echo something on your shell you could use wall:
* * * * * wall <<< "Hello from cron"
* * * * * echo "Hello from cron" | wall
These two lines basically do the same but the first one might not work on older shell, just choose your favorite.
Anyway, be aware that wall will send your message to every user currently connected.
For me * * * * * /bin/echo text > file is not working...I don't know why, previleges and everything is set.
(This command is running normaly when I execute it as the particular
root user, just to clarify this.)
This can be solved by injecting the path PATH=$PATH:/bin in my example.
Instead * * * * * echo text > file is working fine, probably path issue.
Hope I helped

Resources