in reply to Find name of calling script?

If you're running under Unix (Mac is Unix), then the ps -aef command will show running jobs. ON my Mac ...

UID PID PPID C STIME TTY TIME CMD 0 1 0 0 0:01.00 ?? 0:01.38 /sbin/launchd 0 11 1 0 0:00.82 ?? 0:01.04 /usr/libexec/kextd 0 12 1 0 0:07.11 ?? 0:13.22 /usr/sbin/Directory +Service 0 13 1 0 0:01.11 ?? 0:01.64 /usr/sbin/notifyd 0 14 1 0 0:00.73 ?? 0:01.57 /usr/sbin/syslogd ... 0 347 346 0 0:00.02 ttys000 0:00.03 login -pf tomdlux 503 348 347 0 0:00.11 ttys000 0:00.53 -bash 0 3935 348 0 0:00.00 ttys000 0:00.00 ps -aef

You can see that this 'ps' command had Process ID (PID) 3935, and Parent ID (PPID) 348 ... that's a bash shell which was launched by 'login'

use English '-no_match_vars'; will allow you to use $PID or $PROCESS_ID to see your process id, as will getpid(), described in perldoc perlfunc. The parent id is fetched by getppid(). If you're multi-threaded on Linux, you'll want Linux::PID.

If you're on Windows, you're welcome to switch to Unix.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: Find name of calling script?
by cdarke (Prior) on Nov 13, 2010 at 17:04 UTC
    On Windows the Perl getppid is not implemented. I have used the following C code in some of my XS modules:
    #include <windows.h> #include <tlhelp32.h> static int getppid(void) { HANDLE hToolSnapshot; PROCESSENTRY32 Pe32 = {0}; BOOL bResult; DWORD PID; DWORD PPID = 0; PID = GetCurrentProcessId (); hToolSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hToolSnapshot == INVALID_HANDLE_VALUE) { return 0; } Pe32.dwSize = sizeof(PROCESSENTRY32); bResult = Process32First(hToolSnapshot, &Pe32); if (!bResult) return 0; while ( Process32Next(hToolSnapshot, &Pe32) ) { if (Pe32.th32ProcessID == PID) { PPID = Pe32.th32ParentProcessID; break; } } /* Expected */ if (GetLastError() == ERROR_NO_MORE_FILES) { SetLastError(ERROR_SUCCESS); } return PPID; }
    Ah, I see you have upgraded to Linux, maybe it might be useful to some poor Windows coder.