LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2012, 02:06 PM   #1
LegalBreaker
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
Cron using massive amounts of CPU


Hello,

I run a Minecraft server. It's just a Java application. I wrote a bash script that monitored the process and whenever it went down, it starts back up again. I have this set to run every 30 minutes in my crontab.

When I manually run the script, Java uses maybe 30% of my CPU.

When my script detects that the server is down via a cron instance, it uses upwards of 99%. What's going on?

Cron Entry:
*/30 * * * * /root/bin/check.sh

Script:
#!/bin/sh
SERVICE='java'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running, starting minecraft server"
startmc
fi

Startmc script:
cd /var/games/bukkit
./start

Start Script:
java -Xmx1042M -Xms1024M -jar craftbukkit.jar
 
Old 04-17-2012, 03:15 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
You use things in the script (ps, grep) that you don't have full path of the command listed for. When you login you are invoking various profiles for the shell that set environmental variables including PATH. When you run a command in the background (e.g. via init script, udev, cron) it inherits a very minimal environment. Key variables are either not set or not as complete as they are after a login. The main one usually being PATH. You can type "echo $PATH" to see what this is set to. You can type "env" to see all the variables set. You could then add these to your script to output to files and see the difference the script outputs when run as udev as opposed to from command line.

The simplest thing is to put full paths of commands in. Alternatively when you have a lot of them you can simply set the PATH variable at the beginning of the script so it can find all the commands you're using later. (You do still need to do the full path to bash at the #! line because that tells the script to run in bash shell.)

Also the "echo" command is both a shell built in and a binary that can be called. Sometimes you get odd results when you're using one and think you're using the other.
 
Old 04-17-2012, 07:10 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
The ps cmd line is read left to right, so reverse
Code:
grep -v grep | grep $SERVICE

# to 
grep $SERVICE | grep -v grep
to eliminate grep match
 
Old 04-18-2012, 08:35 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Quote:
Originally Posted by chrism01 View Post
The ps cmd line is read left to right, so reverse
Code:
grep -v grep | grep $SERVICE

# to 
grep $SERVICE | grep -v grep
to eliminate grep match
It should work either way though the doing it the first way would make the second grep have to read through far more entries than it would the second way so I agree with your suggestion. I doubt, however, that this is the cause of the long run time the OP described.
 
Old 04-18-2012, 07:30 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Yeah, just a minor thing, it just cleans up the output if you need to parse it.
The timing thing I have no idea
 
Old 04-19-2012, 05:20 AM   #6
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Adding to this you also need to check what all other processes are utilizing cpu processing.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Archiving massive amounts of log data (think petabytes) jeenam Linux - General 8 12-13-2011 10:01 PM
Best utility to copy massive amounts of data king0770 Linux - Hardware 2 07-16-2010 02:45 PM
Searching through massive amounts of Data sxa Linux - Software 5 02-27-2009 10:42 PM
DISCUSSION: Network Attached Storage – An Alternative To Tape Drives In Managing Massive Amounts of Data primearray.com LQ Articles Discussion 0 04-02-2006 05:48 PM


All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration