Visit Our UNIX and Linux User Community


Running two command at the same time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Running two command at the same time
# 1  
Old 11-26-2007
Running two command at the same time

Is there any way I could run two commands at the same time? Say I have in my script a command that grep a keyword from a huge size file:

zgrep $KEYWORD $FILE

and because this is a large file it takes a while to finish, so I would want that while zgrep is doing its job, I have a function that serves a progress bar:

Grepping..

Grepping....

This means that the dot (.) increases simultaneously while zgrep is doing its job internally in my script. Is that possible?
# 2  
Old 11-26-2007
Run the command at background with &.
# 3  
Old 11-26-2007
You didn't say which shell you were using, but something like this should work:

Code:
grep $PAT $FILE > foo.bar &
typeset PID=$!
echo "Processing...\c"
while ps $PID > /dev/null 2>&1; do
  echo ".\c" && sleep 1
done
echo "Done."

The "\c" in the echo command stops the CR/LF (ksh). Other shells probably use "echo -n" instead.
# 4  
Old 11-26-2007
Of course, it just occurred to me that this is an endless loop if the background command never dies. And it's never good to program endless loops. Sooooooo:

Code:
grep $PAT $FILE > foo.bar &
typeset PID=$!
echo "Processing...\c"
typeset START_TIME=$SECONDS  TIMEOUT=1000
while ps $PID > /dev/null 2>&1; do
  echo ".\c" && sleep 1
  if [[ $(((SECONDS-START_TIME))) -gt $TIMEOUT ]]; then
    echo "TIMEOUT! ($TIMEOUT seconds)"
    break
  fi
done
echo "Done."

This way, the loop will exit after 1000 seconds regardless. This probably isn't much of an issue for "grep", but it's a good practice.
# 5  
Old 11-26-2007
But what if the zgrep is not yet done on its job after the 1000 seconds?
# 6  
Old 11-26-2007
I notice that the loop is still infinite.... while in progress I press CTRL+C to quit the script. Then I found out that foo.bar was created almost 2mins ago already.. but still the progress is still running, see below:


Orbix_BOX:/tmp> ./testScript
Mon Nov 26 08:06:44 PST 2007
Processing.......................................................................................... .....................................(CTRL+C)Orbix_BOX:/tmp> date
Mon Nov 26 08:08:57 PST 2007
Orbix_BOX:/tmp> ls -latr foo.bar
-rw-rw---- 1 orbix orbix 137065 Nov 26 08:06 foo.bar
Orbix_BOX:/tmp>
# 7  
Old 11-26-2007
That's because the grep was running in background. If you want to kill it, for instance, you may want to use "trap" to kill it when you press CTRL+C:

Code:
[...]

function ctrlc {
   kill $GREP_PID
}

trap ctrlc INT

[...]

grep $PAT $FILE > foo.bar &
GREP_PID=$!

[...]

Regards.

Last edited by grial; 11-26-2007 at 01:47 PM.. Reason: example code added
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #212
Difficulty: Medium
According to OSPF, an autonomous system boundary router is a router that is connected by using more than one routing protocol and that exchanges routing information with routers autonomous systems.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Many processes running at the same time

Hello everybody , I launched cron to execute a task every hour but the job takes more than hour that's why I'm getting more than 1000 cron processes running at the same time !!! My question is how to tell cron not to execute unless the job terminated in order to have only one process running .... (14 Replies)
Discussion started by: beautymind
14 Replies

2. Shell Programming and Scripting

Calculating the running time

Hi All, I want to run a utility for all the process id that are running for more than 15 mins. I have captured process id's and the time that they were run in a file like below 1st column represnts the process ids and the 2nd one is the Time < 21014 01:00 21099 01:00 24361 01:03 24406... (5 Replies)
Discussion started by: r_t_1601
5 Replies

3. Shell Programming and Scripting

Setting time for running of the script

Dear all, I wonder if it is possible that we can run the script from time to time..I meant, it should repeat the sourcing of the script by itself? In my case, I need to source this script manually from time to time, like once in every 10 minutes. emily, (2 Replies)
Discussion started by: emily
2 Replies

4. Solaris

Please help why my Crontab is not running on time?

I have set up my cron job on the solaris SunOS 5.10 Generic_138888-03 sun4u sparc SUNW,UltraAX-i2 but it is not running on time as expected. Would you please help me to find out what I did wrong? I want to have this cron job run once every month on the 1st Wednesday of the month, but it ran... (6 Replies)
Discussion started by: ggcc
6 Replies

5. Shell Programming and Scripting

Help in running a script after a particular time

Unix Gurus, I have a requirement where the shell script needs to do specific tasks after certain period of time. Daily we receive few files in a particular folder. The script does the file renaming, pass parameters to run some web services and pushes to remote FTP location. But my... (3 Replies)
Discussion started by: shankar1dada
3 Replies

6. Shell Programming and Scripting

Running Cron -- same time jobs

Hi Is it possible to run different cron jobs at the same time? It appears that when I run ones at 15 min granularity that they may prevent ones running later in the day. Should crons run at same time have impact on one another? (4 Replies)
Discussion started by: rob171171
4 Replies

7. Shell Programming and Scripting

Running a process based on time

Hello All, My script is nearly complete, there is just one last piece that needs to be added in. I need to check for the time, and if it is lets say for example. Sunday at 5:00AM, my script cannot run. I would assume it would be something like this, parden the terrible pseudocode ... (7 Replies)
Discussion started by: jeffs42885
7 Replies

8. Solaris

Slow while running a command for the first time

I am facing a performance problem on a Solaris 10 Sparc V890 server, it is an old one I know. The first time we realized there is a problem with the server, is the time when ftp transfers are made. There were 4 other identical servers doing much better. Network drivers are checked and there... (3 Replies)
Discussion started by: royalliege
3 Replies

9. UNIX for Dummies Questions & Answers

Differences between time command and usr/bin/time

I wondered if someone could point out the differences between the time commmand and usr/bin/time and the accuracy one might have over another. Also, is there a website or two a person could maybe link for me to describe the differences? Thank you for your time. (2 Replies)
Discussion started by: icedrake
2 Replies

10. Programming

running a program for a specified time

how can i run a program for a specified time? i need to print a current time during program execution. (3 Replies)
Discussion started by: prosputko
3 Replies

Featured Tech Videos