Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Script Help--Accept Input and Run in BackGround?

I have a shell script in which in the first line I ask the user to input how many minutes they want the script to run for:

 #!/usr/bin/ksh
echo "How long do you want the script to run for in minutes?:\c"
read scriptduration
loopcnt=0
interval=1
date2=$(date +%H:%M%S)
(( intervalsec = $interval * 1 ))
totalmin=${1:-$scriptduration}
(( loopmax = ${totalmin} * 60 ))

ofile=/home2/s499929/test.log
echo "$date2 total runtime is $totalmin minutes at 2 sec intervals"
while(( $loopmax > $loopcnt ))
do
  date1=$(date +%H:%M:%S)
   pid=`/usr/local/bin/lsof | grep 16752 | grep LISTEN |awk '{print $2}'` > /dev/null 2>&1
   count=$(netstat -an|grep 16752|grep ESTABLISHED|wc -l| sed "s/ //g")
   process=$(ps -ef | grep $pid | wc -l | sed "s/ //g")
   port=$(netstat -an | grep 16752 | grep LISTEN | wc -l| sed "s/ //g")
  echo "$date1 activeTCPcount:$count activePID:$pid activePIDcount=$process listen=$port" >> ${ofile}
  sleep $intervalsec
  (( loopcnt = loopcnt + 1 ))
done

It works great if I kick it off an input the values manually. But if I want to run this for 3 hours I need to kick off the script to run in the background.

I have tried just running ./scriptname & and I get this:

$ How long do you want the test to run for in minutes:360
ksh: 360:  not found.
[2] + Stopped (SIGTTIN)        ./test.sh &

And the script dies. Is this possible, any suggestions on how I can accept this one input and then run in the background?? Thanks!!!

like image 507
roacha Avatar asked Aug 10 '11 18:08

roacha


People also ask

How do I keep a shell script running in the background?

Running shell command or script in background using nohup command. Another way you can run a command in the background is using the nohup command. The nohup command, short for no hang up, is a command that keeps a process running even after exiting the shell.

How do I run something in the background in bash?

You can start a background process by appending an ampersand character ( & ) to the end of your commands. This tells the shell not to wait for the process to complete, but instead to begin execution and to immediately return the user to a prompt.

What is $@ in shell script?

$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

Which special command can be added to a script to enable scripts run in the background?

A script can be run in the background by adding a "&" to the end of the script.


1 Answers

You could do something like this:

test.sh arg1 arg2 &

Just refer to arg1 and arg2 as $1 and $2, respectively, in the bash script. ($0 is the name of the script)

So,

test.sh 360 &

will pass 360 as the first argument to the bash or ksh script which can be referred to as $1 in the script.

So the first few lines of your script would now be:

#!/usr/bin/ksh
scriptduration=$1
loopcnt=0
...
...
like image 54
Usagi Avatar answered Sep 28 '22 11:09

Usagi