Register
It is currently Mon Sep 01, 2014 10:20 pm

$! variable and redirection to command


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Jul 25, 2010 4:04 am   

Joined: Sun Jul 25, 2010 3:34 am
Posts: 2
Hello to all board members here,

i'm little bit stuck with one script thing, so I will be very happy, if have some ideas regarding this.
I would like to create simple wrapper to start program in background, pipe its output to syslog and create pidfile.
Basically something like this:

$command 2>&1 | logger -t $syslogtag ($!) &
echo $! > $pidfile

I'm using logger command for redirection to syslog, -t is syslog tag, so it will write something like "mydaemon (pid)" on every line of program output.
So far, so good. But obviously when I would like to write pidfile, value in $! variable is updated by pid of logger process.

I tried few things
- $_ variable normally holds, last argument of last command (so it should be "(pid)"), but it won't work for me.
- any subcommands for storing of command pid to some variable broke redirection or sending to background
- pid of subsequent command is usually incremented by one, so theoretically i could subtract one to get first pid, but it is controlled by OS and i can't rely on this.

Do you know some way, how to solve it?

Thank you

Michal Smucr


Top
 Profile  
 PostPosted: Sun Jul 25, 2010 7:45 am   

Joined: Sun Jul 25, 2010 3:34 am
Posts: 2
Hello,

it seems, that i got one trick.
I found on man page of bash, that jobs builtin ivocation:
jobs -p List only the process ID of the job’s process group leader.

I my case with backgrounded job, first command is process group leader, so
echo $(jobs -p) > $pidfile
works fine.

Best regards

Michal Smucr


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP