How to get the path of a current running script file? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionHow to get the full executable name of a running process in LinuxWhat can make passing init=/path/to/program to the kernel not start program as init?Is there an easy way to log all commands executed, including command line arguments?Perl and Python wrongly interpreted via shebang on LinuxAbout executing a python program in a shell script which is scheduled on crontabWhy does a process of a binary with only execute permission remain hidden in “ps” when using hidepid=2, if the user is not root?How to disable useless “audit success” log entries in dmesgHow to get the full executable name of a running process in LinuxHow to get the actual program name using the PID of that running program?Running python script on ubuntu machine using ./myscript.pyUpstart script with expect scripting

Generate an RGB colour grid

How does light 'choose' between wave and particle behaviour?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How fail-safe is nr as stop bytes?

Why limits give us the exact value of the slope of the tangent line?

Why do early math courses focus on the cross sections of a cone and not on other 3D objects?

How does the math work when buying airline miles?

How to make a Field only accept Numbers in Magento 2

What is "gratricide"?

Is CEO the "profession" with the most psychopaths?

Effects on objects due to a brief relocation of massive amounts of mass

How to compare two different files line by line in unix?

Can anything be seen from the center of the Boötes void? How dark would it be?

How does the secondary effect of the Heat Metal spell interact with a creature resistant/immune to fire damage?

Can a new player join a group only when a new campaign starts?

How often does castling occur in grandmaster games?

Put R under double integral

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Question about debouncing - delay of state change

What does it mean that physics no longer uses mechanical models to describe phenomena?

How to improve on this Stylesheet Manipulation for Message Styling

What do you call the main part of a joke?

"Lost his faith in humanity in the trenches of Verdun" — last line of an SF story

Is this another way of expressing the side limit?



How to get the path of a current running script file?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionHow to get the full executable name of a running process in LinuxWhat can make passing init=/path/to/program to the kernel not start program as init?Is there an easy way to log all commands executed, including command line arguments?Perl and Python wrongly interpreted via shebang on LinuxAbout executing a python program in a shell script which is scheduled on crontabWhy does a process of a binary with only execute permission remain hidden in “ps” when using hidepid=2, if the user is not root?How to disable useless “audit success” log entries in dmesgHow to get the full executable name of a running process in LinuxHow to get the actual program name using the PID of that running program?Running python script on ubuntu machine using ./myscript.pyUpstart script with expect scripting



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Knowing that program's PID, I want to list out the executable file of a running program.



For binary programs, I can read /proc/$pid/exe, which is a symlink to the executable.



However for scripting programs, for example a Python program, /proc/$pid/exe points to /usr/bin/python, not the program itself. This happens no matter I started the program with ./program.py or with python program.py.



I tried several ways to solve it:



/proc/$pid/cmdline contains the filename of the script, but it is in relative path and the working directory may have changed already.



I also tried /proc/$pid/comm but it can be easily changed by the program.



So how to know the original script file of a running program?










share|improve this question






















  • echo 'puts "what script file?"' | ruby -

    – thrig
    Apr 6 '16 at 18:27











  • ps -ef | grep "$pid" should give you the full command for the process including what script is being run and what options were given. But the output is dependent on what is running and how it was called. Don't know if this will answer your question so posting as comment.

    – Munir
    Apr 6 '16 at 18:32











  • @Munir ps -ef is the same as /proc/$pid/comm. The problem is the filename is relative path, not absolute path.

    – Star Brilliant
    Apr 6 '16 at 18:35











  • @StarBrilliant They are not the same...Example - I have tuned running on my RHEL server as PID 16865. ps -ef gives me /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf but cat /proc/16865/comm just says tuned.

    – Munir
    Apr 6 '16 at 18:42












  • @Munir It's my typo. I meant /proc/$pid/cmdline. It's cmdline instead of comm.

    – Star Brilliant
    Apr 7 '16 at 12:35


















0















Knowing that program's PID, I want to list out the executable file of a running program.



For binary programs, I can read /proc/$pid/exe, which is a symlink to the executable.



However for scripting programs, for example a Python program, /proc/$pid/exe points to /usr/bin/python, not the program itself. This happens no matter I started the program with ./program.py or with python program.py.



I tried several ways to solve it:



/proc/$pid/cmdline contains the filename of the script, but it is in relative path and the working directory may have changed already.



I also tried /proc/$pid/comm but it can be easily changed by the program.



So how to know the original script file of a running program?










share|improve this question






















  • echo 'puts "what script file?"' | ruby -

    – thrig
    Apr 6 '16 at 18:27











  • ps -ef | grep "$pid" should give you the full command for the process including what script is being run and what options were given. But the output is dependent on what is running and how it was called. Don't know if this will answer your question so posting as comment.

    – Munir
    Apr 6 '16 at 18:32











  • @Munir ps -ef is the same as /proc/$pid/comm. The problem is the filename is relative path, not absolute path.

    – Star Brilliant
    Apr 6 '16 at 18:35











  • @StarBrilliant They are not the same...Example - I have tuned running on my RHEL server as PID 16865. ps -ef gives me /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf but cat /proc/16865/comm just says tuned.

    – Munir
    Apr 6 '16 at 18:42












  • @Munir It's my typo. I meant /proc/$pid/cmdline. It's cmdline instead of comm.

    – Star Brilliant
    Apr 7 '16 at 12:35














0












0








0








Knowing that program's PID, I want to list out the executable file of a running program.



For binary programs, I can read /proc/$pid/exe, which is a symlink to the executable.



However for scripting programs, for example a Python program, /proc/$pid/exe points to /usr/bin/python, not the program itself. This happens no matter I started the program with ./program.py or with python program.py.



I tried several ways to solve it:



/proc/$pid/cmdline contains the filename of the script, but it is in relative path and the working directory may have changed already.



I also tried /proc/$pid/comm but it can be easily changed by the program.



So how to know the original script file of a running program?










share|improve this question














Knowing that program's PID, I want to list out the executable file of a running program.



For binary programs, I can read /proc/$pid/exe, which is a symlink to the executable.



However for scripting programs, for example a Python program, /proc/$pid/exe points to /usr/bin/python, not the program itself. This happens no matter I started the program with ./program.py or with python program.py.



I tried several ways to solve it:



/proc/$pid/cmdline contains the filename of the script, but it is in relative path and the working directory may have changed already.



I also tried /proc/$pid/comm but it can be easily changed by the program.



So how to know the original script file of a running program?







linux proc shebang






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 6 '16 at 18:18









Star BrilliantStar Brilliant

1013




1013












  • echo 'puts "what script file?"' | ruby -

    – thrig
    Apr 6 '16 at 18:27











  • ps -ef | grep "$pid" should give you the full command for the process including what script is being run and what options were given. But the output is dependent on what is running and how it was called. Don't know if this will answer your question so posting as comment.

    – Munir
    Apr 6 '16 at 18:32











  • @Munir ps -ef is the same as /proc/$pid/comm. The problem is the filename is relative path, not absolute path.

    – Star Brilliant
    Apr 6 '16 at 18:35











  • @StarBrilliant They are not the same...Example - I have tuned running on my RHEL server as PID 16865. ps -ef gives me /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf but cat /proc/16865/comm just says tuned.

    – Munir
    Apr 6 '16 at 18:42












  • @Munir It's my typo. I meant /proc/$pid/cmdline. It's cmdline instead of comm.

    – Star Brilliant
    Apr 7 '16 at 12:35


















  • echo 'puts "what script file?"' | ruby -

    – thrig
    Apr 6 '16 at 18:27











  • ps -ef | grep "$pid" should give you the full command for the process including what script is being run and what options were given. But the output is dependent on what is running and how it was called. Don't know if this will answer your question so posting as comment.

    – Munir
    Apr 6 '16 at 18:32











  • @Munir ps -ef is the same as /proc/$pid/comm. The problem is the filename is relative path, not absolute path.

    – Star Brilliant
    Apr 6 '16 at 18:35











  • @StarBrilliant They are not the same...Example - I have tuned running on my RHEL server as PID 16865. ps -ef gives me /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf but cat /proc/16865/comm just says tuned.

    – Munir
    Apr 6 '16 at 18:42












  • @Munir It's my typo. I meant /proc/$pid/cmdline. It's cmdline instead of comm.

    – Star Brilliant
    Apr 7 '16 at 12:35

















echo 'puts "what script file?"' | ruby -

– thrig
Apr 6 '16 at 18:27





echo 'puts "what script file?"' | ruby -

– thrig
Apr 6 '16 at 18:27













ps -ef | grep "$pid" should give you the full command for the process including what script is being run and what options were given. But the output is dependent on what is running and how it was called. Don't know if this will answer your question so posting as comment.

– Munir
Apr 6 '16 at 18:32





ps -ef | grep "$pid" should give you the full command for the process including what script is being run and what options were given. But the output is dependent on what is running and how it was called. Don't know if this will answer your question so posting as comment.

– Munir
Apr 6 '16 at 18:32













@Munir ps -ef is the same as /proc/$pid/comm. The problem is the filename is relative path, not absolute path.

– Star Brilliant
Apr 6 '16 at 18:35





@Munir ps -ef is the same as /proc/$pid/comm. The problem is the filename is relative path, not absolute path.

– Star Brilliant
Apr 6 '16 at 18:35













@StarBrilliant They are not the same...Example - I have tuned running on my RHEL server as PID 16865. ps -ef gives me /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf but cat /proc/16865/comm just says tuned.

– Munir
Apr 6 '16 at 18:42






@StarBrilliant They are not the same...Example - I have tuned running on my RHEL server as PID 16865. ps -ef gives me /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf but cat /proc/16865/comm just says tuned.

– Munir
Apr 6 '16 at 18:42














@Munir It's my typo. I meant /proc/$pid/cmdline. It's cmdline instead of comm.

– Star Brilliant
Apr 7 '16 at 12:35






@Munir It's my typo. I meant /proc/$pid/cmdline. It's cmdline instead of comm.

– Star Brilliant
Apr 7 '16 at 12:35











1 Answer
1






active

oldest

votes


















0














Can we assume the files are not changed on disk (at least, no files with the same name added or deleted) and /proc/[pid]/cmdline is reliable (see side note below)?



According to proc(5): /proc/[pid]/environ contains the initial environment variables (which won't be changed even if the program itself changes the envrionment variables inside) including PWD and PATH.



Therefore:

if the path in cmdline starts with relative dirname(s), you can use PWD (in /proc/[pid]/environ) as the base path and resolve the relative path;

if the path in cmdline is the name of the program itself, you can loop through each directory in PATH (in /proc/[pid]/environ) and the target is the first file with the same name.




Side notes:



/proc/[pid]/exe seems to be the dereferenced file (e.g. /usr/bin/python3.6 instead of /usr/bin/python3 or /usr/bin/python if they are symlinks);



/proc/[pid]/cmdline can also contain strange information for some programs so may be unreliable. This seems to be related to the program but not the kernel, and I didn't observe any python scripts behaving this way (and editing sys.argv in python doesn't seem to affect /proc/[pid]/cmdline). My question also talked a bit about this.






share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f274759%2fhow-to-get-the-path-of-a-current-running-script-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Can we assume the files are not changed on disk (at least, no files with the same name added or deleted) and /proc/[pid]/cmdline is reliable (see side note below)?



    According to proc(5): /proc/[pid]/environ contains the initial environment variables (which won't be changed even if the program itself changes the envrionment variables inside) including PWD and PATH.



    Therefore:

    if the path in cmdline starts with relative dirname(s), you can use PWD (in /proc/[pid]/environ) as the base path and resolve the relative path;

    if the path in cmdline is the name of the program itself, you can loop through each directory in PATH (in /proc/[pid]/environ) and the target is the first file with the same name.




    Side notes:



    /proc/[pid]/exe seems to be the dereferenced file (e.g. /usr/bin/python3.6 instead of /usr/bin/python3 or /usr/bin/python if they are symlinks);



    /proc/[pid]/cmdline can also contain strange information for some programs so may be unreliable. This seems to be related to the program but not the kernel, and I didn't observe any python scripts behaving this way (and editing sys.argv in python doesn't seem to affect /proc/[pid]/cmdline). My question also talked a bit about this.






    share|improve this answer





























      0














      Can we assume the files are not changed on disk (at least, no files with the same name added or deleted) and /proc/[pid]/cmdline is reliable (see side note below)?



      According to proc(5): /proc/[pid]/environ contains the initial environment variables (which won't be changed even if the program itself changes the envrionment variables inside) including PWD and PATH.



      Therefore:

      if the path in cmdline starts with relative dirname(s), you can use PWD (in /proc/[pid]/environ) as the base path and resolve the relative path;

      if the path in cmdline is the name of the program itself, you can loop through each directory in PATH (in /proc/[pid]/environ) and the target is the first file with the same name.




      Side notes:



      /proc/[pid]/exe seems to be the dereferenced file (e.g. /usr/bin/python3.6 instead of /usr/bin/python3 or /usr/bin/python if they are symlinks);



      /proc/[pid]/cmdline can also contain strange information for some programs so may be unreliable. This seems to be related to the program but not the kernel, and I didn't observe any python scripts behaving this way (and editing sys.argv in python doesn't seem to affect /proc/[pid]/cmdline). My question also talked a bit about this.






      share|improve this answer



























        0












        0








        0







        Can we assume the files are not changed on disk (at least, no files with the same name added or deleted) and /proc/[pid]/cmdline is reliable (see side note below)?



        According to proc(5): /proc/[pid]/environ contains the initial environment variables (which won't be changed even if the program itself changes the envrionment variables inside) including PWD and PATH.



        Therefore:

        if the path in cmdline starts with relative dirname(s), you can use PWD (in /proc/[pid]/environ) as the base path and resolve the relative path;

        if the path in cmdline is the name of the program itself, you can loop through each directory in PATH (in /proc/[pid]/environ) and the target is the first file with the same name.




        Side notes:



        /proc/[pid]/exe seems to be the dereferenced file (e.g. /usr/bin/python3.6 instead of /usr/bin/python3 or /usr/bin/python if they are symlinks);



        /proc/[pid]/cmdline can also contain strange information for some programs so may be unreliable. This seems to be related to the program but not the kernel, and I didn't observe any python scripts behaving this way (and editing sys.argv in python doesn't seem to affect /proc/[pid]/cmdline). My question also talked a bit about this.






        share|improve this answer















        Can we assume the files are not changed on disk (at least, no files with the same name added or deleted) and /proc/[pid]/cmdline is reliable (see side note below)?



        According to proc(5): /proc/[pid]/environ contains the initial environment variables (which won't be changed even if the program itself changes the envrionment variables inside) including PWD and PATH.



        Therefore:

        if the path in cmdline starts with relative dirname(s), you can use PWD (in /proc/[pid]/environ) as the base path and resolve the relative path;

        if the path in cmdline is the name of the program itself, you can loop through each directory in PATH (in /proc/[pid]/environ) and the target is the first file with the same name.




        Side notes:



        /proc/[pid]/exe seems to be the dereferenced file (e.g. /usr/bin/python3.6 instead of /usr/bin/python3 or /usr/bin/python if they are symlinks);



        /proc/[pid]/cmdline can also contain strange information for some programs so may be unreliable. This seems to be related to the program but not the kernel, and I didn't observe any python scripts behaving this way (and editing sys.argv in python doesn't seem to affect /proc/[pid]/cmdline). My question also talked a bit about this.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 29 '17 at 9:20

























        answered May 28 '17 at 17:29









        renyuneyunrenyuneyun

        55210




        55210



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f274759%2fhow-to-get-the-path-of-a-current-running-script-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.