How to read grep command result in linux? - linux

I want to monitor jboss server status in linux for keep up & running the application.My requirement is if server down , i need to get the email from server.
So i'm planning to run crontab in linux for every hour or half an hour to check the server status.
30 * * * * ps -ef|grep java | mail test#domain.com
I' will get grep command result when running above command. So How to read the grep command result to find server is up & running ?
Please advise if anybody have alternative.

In your crontab file write this:
MAILTO="test#domain.com"
30 * * * * pgrep -lf java

At the beginning of cron add
MAILTO="test#domain.com"
This would send the output of commands in cron. You dont require to pipe the output to mail again. (reference)

Related

Cronjob for reboot not working

I am trying crontab but crontab for email is not working.However, I want that crontab runs a shell script and that shell script runs a python script.
One crontab I tried is
* * * * * echo " This is current date and time $(date)"
but this is not getting printed on screen. I don't understand what is wrong I am doing.
for sending the email form this command.
echo " This is a message " | mailx -s "Subject" mymail#email.com
you need to make sure port 25 is open on your system.
you can use the mailq command for check email in the queue on your system.
you can check local port is open or not from
telnet localhost 25
nc localhost 25

Cron job is not triggered on Ubuntu VM

Ubuntu
In my Ubuntu VM, I have configured a cronjob
cat /var/spool/cron/crontabs/*
MAILTO="myemail#gmail.com"
* * * * * python /home/forge/web-app/database/backup_mysql.py
I checked pgrep cron I got number printing out fine.
It been 5 mins now, I don't see any email send to me.
I don't see any backup file is being generated.
I have a feeling that this cronjob never got run.
How do I debug this ?
Do I need to restart some kind of service ?
Could you please check the cron service.
service cron status.
And check the cronjob logs to check it is running or not
tail -f /var/log/cron | grep username
Check the cron
crontab -e -u username
And also check permission.
chmod +x <file>

Active cron job

I am trying to make a cron job for the first time but i have some problems making it work.
Here is what i have done so far:
Linux commands:
crontab -e
My cronjob looks like this:
1 * * * * wget -qO /dev/null http://mySite/myController/myView
Now when i look in:
/var/spool/cron/crontabs/
I get the following output:
marc root
if i open the file root
i see my cronjob (the one above)
However it doesnt seem like it is running.
is there a way i can check if its running or make sure that it is running?
By default cron jobs do have a log file. It should be in /var/log/syslog (depends on your system). Vouch for it and you're done. Else you can simply append the output to a log file manually by
1 * * * * wget http://mySite/myController/myView >> ~/my_log_file.txt
and see what's your output. Notice I've changed removed the quiet parameter from wget command so that there is some output.

Linux cronjob doesn't work (execute script)

I created a cronjob with the command crontab -e:
*/1 * * * * /var/lib/tomcat/webapps/ROOT/WEB-INF/scripts/test.sh
This file test.sh should be executed every minute. But it doesn't work.
If I run the script manually it works fine. So I think the problem is the cronjob not the script ;)
Are there any permissions or something else which block the cronjob?
Is the cronjob syntax correct?
Thx
For a start, you don't need the /1 if you want it done every minute. Just setting the minute field to * will do.
Next, you should place, as the first lines in your test script (though after the #! line if it's there):
env >/tmp/test.sh.dummy
set >>/tmp/test.sh.dummy
and see if that file shows up.
That will tell you if the script is running or not.
If it's not running, check to make sure cron itself is running:
pax> ps -ef | grep cron | grep -v grep
root 1048 1 0 08:45 ? 00:00:00 cron
(mine is).
If it is running, the most likely problem is that the environment under which cron runs your jobs is nowhere near the environment your shell gives you. Examine the differences between what was output to your /tmp/test.sh.dummy file and what your shell gives your when you execute env ; set.

Shell script to log server checks runs manually, but not from cron

I'm using a basic shell script to log the results of top, netstat, ps and free every minute.
This is the script:
/scripts/logtop:
TERM=vt100
export TERM
time=$(date)
min=${time:14:2}
top -b -n 1 > /var/log/systemCheckLogs/$min
netstat -an >> /var/log/systemCheckLogs/$min
ps aux >> /var/log/systemCheckLogs/$min
free >> /var/log/systemCheckLogs/$min
echo "Message Content: $min" | mail -s "Ran System Check script" email#domain.com
exit 0
When I run this script directly it works fine. It creates the files and puts them in /var/log/systemCheckLogs/ and then sends me an email.
I can't, however, get it to work when trying to get cron to do it every minute.
I tried putting it in /var/spool/cron/root like so:
* * * * * /scripts/logtop > /dev/null 2>&1
and it never executes
I also tried putting it in /var/spool/cron/myservername and also like so:
* * * * * /scripts/logtop > /dev/null 2>&1
it'll run every minute, but nothing gets created in systemCheckLogs.
Is there a reason it works when I run it but not when cron runs it?
Also, here's what the permissions look like:
-rwxrwxrwx 1 root root 326 Jul 21 01:53 logtop
drwxr-xr-x 2 root root 4096 Jul 21 01:51 systemCheckLogs
Normally crontabs are kept in "/var/spool/cron/crontabs/". Also, normally, you update it with the crontab command as this HUPs crond after you're done and it'll make sure the file gets in the correct place.
Are you using the crontab command to create the cron entry? crontab to import a file directly. crontab -e to edit the current crontab with $EDITOR.
All jobs run by cron need the interpreter listed at the top, so cron knows how to run them.
I can't tell if you just omitted that line or if it is not in your script.
For example,
#!/bin/bash
echo "Test cron jon"
When running from /var/spool/cron/root, it may be failing because cron is not configured to run for root. On linux, root cron jobs are typically run from /etc/crontab rather than from /var/spool/cron.
When running from /var/spool/cron/myservername, you probably have a permissions problem. Don't redirect the error to /dev/null -- capture them and examine.
Something else to be aware of, cron doesn't initialize the full run environment, which can sometimes mean you can run it just fine from a fully logged-in shell, but it doesn't behave the same from cron.
In the case of above, you don't have a "#!/bin/shell" up top in your script. If root is configured to use something like a regular bourne shell or cshell, the syntax you use to populate your variables will not work. This would explain why it would run, but not populate your files. So if you need it to be ksh, "#!/bin/ksh". It's generally best not to trust the environment to keep these things sane. If you need your profile run the do a ". ~/.profile" up front as well. Or a quick and dirty way to get your relatively full env is to do it from su as such "* * * * * su - root -c "/path/to/script" > /dev/null 2>&1
Just some things I've picked up over the years. You're definitely expecting a ksh based on your syntax, so you might want to be sure it's using it.
Thanks for the tips... used a little bit of each answer to get to the bottom of this.
I did have the interpreter at the top (wasn't shown here), but may have been wrong.
Am using #!/bin/bash now and that works.
Also had to tinker with the permissions of the directory the log files are being dumped in to get things working.

Resources