How can I put the process id of ,for example, the following script to the pid file? Maybe $$ must be defined in some place?
daemon(){
while true;
do
if [ -r "${FILE}" ]
then
echo "`date +"%H:%M:%S"`" >> $FILE
sleep 3;
fi
done
}
daemon >/dev/null 2>&1 &
FYI: $$ is the pid of the current process, so:
echo "`date +"%H:%M:%S"` $$" >> $FILE
does write the pid to $FILE, but it is a child process because the & runs the function in the background.
Good point... You may be able to 'source' the command by prepending a period[SPACE] so it runs in the same process/shell.
Untested but worth a shot:
. echo "`date +"%H:%M:%S"` $$" >> $FILE
I think the "daemon" is meant to run by itself in a child process. It doesn't do much.
Maybe they use to track usage of the script that runs it...
This example script works perfect only in following configuration:
daemon(){
echo $$ > ${PF}
while true;
do
if [ -r "${FILE}" ]
then
echo "`date +"%H:%M:%S"`" >> $FILE
sleep 3;
fi
done
}
#MAIN
case $1 in
-daemon)
daemon
;;
*)
$0 -daemon &
;;
esac
exit 0