LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-13-2012, 02:29 PM   #1
seway
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
Need help with Bash Script


Hello guys im trying to make a bash script that will run a program and sleep for 15 seconds running the program and it will stop the program and sleep 300 seconds and than start the program again, here the code i have but its not working:

Quote:
#!/bin/bash

PID=""

function get_pid {
PID=`ps ax |grep ata_pep |cut -d " " -f 1`
}

function start {
get_pid
if [ -z $PID ]; then
echo "Starting Ata Pep.."
nice -n -20 /prog/ata_pep xxx.xxx.xxx.xxx &
get_pid
echo "Done. PID=$PID"
sleep 15
kill $PID
sleep 300
start
fi
}
And im running the script using: ./nameofthescript start

I hope someone can help me with this issue.

Thanks a lot for all the attention.
 
Old 04-13-2012, 03:02 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,339

Rep: Reputation: Disabled
You could use $!:

#!/bin/bash

Code:
echo "Starting Ata Pep.."
while [ 1 ] ; do
  nice -n -20 /prog/ata_pep xxx.xxx.xxx.xxx &
  PID=$!
  echo "Done. PID=$PID"
  sleep 15
  kill $PID
  sleep 300
done
 
Old 04-13-2012, 03:04 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You have the function named "start" but then you call "start" from within the function definition. It would need to be called AFTER the function definition. That is to say functions don't get executed when defined but rather when called later in the script. (e.g. calling get_pid in your start function is valid because you previously defined get_pid.)
 
Old 04-13-2012, 03:06 PM   #4
seway
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks a lot guys i found out the problem and now its working perfect.

This topic can be post as solved.
 
Old 04-13-2012, 03:12 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Well as written, the script sets variable PID to null, defines a function "get_pid", defines a function "start", and then exits without ever calling either function. The parameter that you passed when invoking the script is never used.

Once you correct that by inserting a call to "start" (perhaps with a test of the supplied parameter, "$1") following the function definitions, you will find that the script will run for quite a while but will eventually run out of memory due to the infinite recursion of the "start" function calling itself. Rather than doing that, you should just write the body of the "start" function as an infinite loop
Code:
while :; do
    ...
done
Also, if your installation includes the pgrep command, the function "get_pid" could be deleted and the call replaced by
Code:
PID=`pgrep -x ata_pep`
See the manpage for pgrep for details. (I'll leave the possibility of more that one "ata_pep" process running simultaneously as an exercise, but you might want to look at the "-P ppid" option of the pgrep command.)
 
  


Reply



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
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:32 PM.

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
Open Source Consulting | Domain Registration