SUMMARY: find out one's own PID when running from cron



I've already got a summary:

If your script is sh or ksh, so $$ will contain the PID of your
running script.

So I just did a:

export MY_PID=$$


And it worked PERFECTLY!

Thanks to Jean-Marc Vincent at HP.

Andy



ORIGINAL QUESTION
=================

Hi,

I have a shell script that runs from cron. It needs to check if there
are still previous runs of itself still running and if so it needs to
kill them (but not kill itself). I can get the shell script to find all
currently running copies of this script but the list includes the
current one but I only want to kill the previous ones. How do I get the
shell script to find out its own PID? I can do it at the command line
but when I try to do it when the script runs from cron I get an error
message:

ps: no controlling terminal

Thanks,
Andy



Relevant Pages

  • Re: LTT user input
    ... and to that end have taken the existing trace infrastructure ... script for that event (if there's a handler defined for the event ... This gives the script a chance to do whatever Perlish thing it ... syscall totals to individual syscall totals for each pid. ...
    (Linux-Kernel)
  • Re: rc.subr question
    ... kill: 19790: No such process ... What is the right way to describe such daemon in rc script? ... The best way is to record fmsadmin's primary pid to a file, ...
    (freebsd-questions)
  • Re: Bash script problem
    ... > I have a problem with a bash script. ... > never used the correct pid... ... You probably want to kill the process _group_, ... interpret it as a process group (for this to work, ...
    (Debian-User)
  • Re: [SLE] Limiting user internet usage by duration. Is it possible?
    ... launch a script in the background that will kill the pid after ... It will trigger if the user is reading local documentation files. ...
    (SuSE)
  • Re: A Linux timed audio recorder
    ... sleep $X # wait for some time ... kill $Pid # hoping that noone re-used that PID in the meantime... ... So here a nice example that uses a longer script then just one line: ...
    (alt.os.linux.suse)