Operating System - HP-UX
1755960 Members
4552 Online
108839 Solutions
New Discussion юеВ

run command concurrently within script

 
SOLVED
Go to solution
yc_2
Regular Advisor

run command concurrently within script

Hi,

Is it possible to write a script to run multiple commands concurrently? Example: perform fsck for a few file systems.

Thanks in advance
7 REPLIES 7
Steven E. Protter
Exalted Contributor
Solution

Re: run command concurrently within script

Shalom,

Yes


#!/bin/ksh

command1 &
command2 &
command3 &



Doing this with fsck might cause I/O to go high, but it should work.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
yc_2
Regular Advisor

Re: run command concurrently within script

Any way to capture the output since it is in back ground?
Torsten.
Acclaimed Contributor

Re: run command concurrently within script

Try

# command1 >/tmp/command1.out 2>/tmp/command1.out &

BTW, fsck on a newly created filesystem is maybe not needed and will return very quickly.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
yc_2
Regular Advisor

Re: run command concurrently within script

Thanks. I will try it out. The fsck is use for true copy purpose.
yc_2
Regular Advisor

Re: run command concurrently within script

Why pipe 2 times to the same file?

# command1 >/tmp/command1.out 2>/tmp/command1.out &
Torsten.
Acclaimed Contributor

Re: run command concurrently within script

One ">" was missing, sorry.

# command1 >/tmp/command1.out 2>>/tmp/command1.out &

But you can use different files, of course.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Bill Hassell
Honored Contributor

Re: run command concurrently within script

> # command1 >/tmp/command1.out 2>>/tmp/command1.out &

This is a better way if all output (stdout and stderr must go into the same file):

cmd1 > /tmp/cmd1.log 2>&1 &
cmd2 > /tmp/cmd2.log 2>&1 &

The construct 2>&1 says: take stderr (file number 2) and redirect it into file number 1. Each time the command runs, it will clear the log file. To track all runs of the command, change > to >> as in:

cmd1 >> /tmp/cmd1.log 2>&1 &
cmd2 >> /tmp/cmd2.log 2>&1 &

For cumulative logs, be sure to add a timestamp, especially one that can be easily sorted or selected such as YYYYMMDD.HHMMSS. You can add this to your scripts:

echo "$(date +%Y%m%d.%H%M%S) and sime text..."

Or for applications that you can't modify, add the timestamp before running the program:

echo "$(date +%Y%m%d.%H%M%S)" >> cmd1.log; cmd1 >> /tmp/cmd1.log 2>&1 &
echo "$(date +%Y%m%d.%H%M%S)" >> cmd1.log; cmd2 >> /tmp/cmd2.log 2>&1 &


Bill Hassell, sysadmin