ruby - How to run a system command, and pipe PID to pidfile? -
how run command , pipe resulting pid pidfile?
i'm looking unix command, if there's way in ruby, that'd nice too.
i tried:
bundle exec clockwork clock_local.rb echo $! > #{current_path}/tmp/pids/clockwork.pid
and seems pipe entire output pidfile rather pid.
you can use ruby's begin {...}
, end {...}
automatically create , delete pid files. add these ruby script:
begin { file.write("#{ $0 }.pid", $$) } end { file.delete("#{ $0 }.pid") }
you test create simple pid file simple script like:
begin { file.write("#{ $0 }.pid", $$) } end { file.delete("#{ $0 }.pid") } sleep 60
run it, then, while it's running check directory it's in pid file. after 1 minute, when sleep
expires pid file should automatically disappear.
those don't exception handling in case file isn't writable or if exists , locked. end
fire off if script receives cntrl+c , automatically remove pid file.
additional things you'd want add tests existing pid file, locking file after creation can't overwritten, done keeping open exclusive lock (see flock
).
Comments
Post a Comment