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











up vote
0
down vote

favorite












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














bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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

















up vote
0
down vote

favorite












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














bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 Brilliant

1013




1013





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 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

















up vote
0
down vote













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',
    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








    up vote
    0
    down vote













    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



























      up vote
      0
      down vote













      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

























        up vote
        0
        down vote










        up vote
        0
        down vote









        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









        renyuneyun

        5010




        5010






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt