In a BASH library I'm creating, I have two functions that look like:
function check_process {
PIDFILE=$1
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ ! "$PID" == "" ] && [ -d "/proc/$PID" ]; then
return 1
fi;
fi;
return 0
}
function fork_process {
CMD=$1
PIDFILE=$2
if check_process $PIDFILE; then
$CMD &
echo $! > $PIDFILE
echo "new pid in $PIDFILE on `date`" >> $LOG
fi;
}
Which I use to launch, keep track of the PID, and prevent duplicates of any process in a system independent manner (there exists utilities like start-stop-daemon and startproc on different linux flavors, but they are not universal).
So if I want to use it to track a process over an SSH connection, I could do something like:
fork_process "ssh some_user@some_server tail -f some_log_file >> local_log_file" my_process.pid
This would check the file my_process.pid and make sure that the process ID is not already running before going ahead to run the SSH connection and fork it in the background, while saving the new PID in the pid file.
The problem is, if I have a command like this:
tail -f local_log_file | gawk my_gawk.awk >> some_other_log
then doing:
fork_process "tail -f local_log_file | gawk my_gawk.awk >> some_other_log" my_process.pid
will not work, because BASH will interpret the '|' character literally and not as a pipe. Is there a way to force BASH to interpret '|' as a pipe even when it is in a string?
Thanks,
Neked