pkill -P variable not working when running .sh under cron

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a very simple .sh script to kill a process. The processes saves its PID to pid2.txt.



pkill -e -P $(cat pid2.txt)
exit


When I run this as sudo ./myscript.sh from the command line it works fine.



When run by cron as */2 * * * * /etc/mylocation/myscript.sh it throws an error:



cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'


What am I doing wrong?







share|improve this question

















  • 3




    Add the full path of pid2.txt in your script, it will then work
    – Arushix
    Jun 21 at 5:32











  • Great, I will add it as the answer
    – Arushix
    Jun 21 at 5:42










  • I'm unfamiliar with the -e flag for pkill. What does it do on your system, and what Unix are you using?
    – Kusalananda
    Jun 21 at 6:02










  • Ubuntu 16.04. -e echoes which PID was killed.
    – sjaak
    Jun 21 at 7:02














up vote
0
down vote

favorite












I have a very simple .sh script to kill a process. The processes saves its PID to pid2.txt.



pkill -e -P $(cat pid2.txt)
exit


When I run this as sudo ./myscript.sh from the command line it works fine.



When run by cron as */2 * * * * /etc/mylocation/myscript.sh it throws an error:



cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'


What am I doing wrong?







share|improve this question

















  • 3




    Add the full path of pid2.txt in your script, it will then work
    – Arushix
    Jun 21 at 5:32











  • Great, I will add it as the answer
    – Arushix
    Jun 21 at 5:42










  • I'm unfamiliar with the -e flag for pkill. What does it do on your system, and what Unix are you using?
    – Kusalananda
    Jun 21 at 6:02










  • Ubuntu 16.04. -e echoes which PID was killed.
    – sjaak
    Jun 21 at 7:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a very simple .sh script to kill a process. The processes saves its PID to pid2.txt.



pkill -e -P $(cat pid2.txt)
exit


When I run this as sudo ./myscript.sh from the command line it works fine.



When run by cron as */2 * * * * /etc/mylocation/myscript.sh it throws an error:



cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'


What am I doing wrong?







share|improve this question













I have a very simple .sh script to kill a process. The processes saves its PID to pid2.txt.



pkill -e -P $(cat pid2.txt)
exit


When I run this as sudo ./myscript.sh from the command line it works fine.



When run by cron as */2 * * * * /etc/mylocation/myscript.sh it throws an error:



cat: pid2.txt: No such file or directory pkill: option requires an argument -- 'P'


What am I doing wrong?









share|improve this question












share|improve this question




share|improve this question








edited Jun 21 at 8:57









Anthony Geoghegan

7,15233651




7,15233651









asked Jun 21 at 5:29









sjaak

182




182







  • 3




    Add the full path of pid2.txt in your script, it will then work
    – Arushix
    Jun 21 at 5:32











  • Great, I will add it as the answer
    – Arushix
    Jun 21 at 5:42










  • I'm unfamiliar with the -e flag for pkill. What does it do on your system, and what Unix are you using?
    – Kusalananda
    Jun 21 at 6:02










  • Ubuntu 16.04. -e echoes which PID was killed.
    – sjaak
    Jun 21 at 7:02












  • 3




    Add the full path of pid2.txt in your script, it will then work
    – Arushix
    Jun 21 at 5:32











  • Great, I will add it as the answer
    – Arushix
    Jun 21 at 5:42










  • I'm unfamiliar with the -e flag for pkill. What does it do on your system, and what Unix are you using?
    – Kusalananda
    Jun 21 at 6:02










  • Ubuntu 16.04. -e echoes which PID was killed.
    – sjaak
    Jun 21 at 7:02







3




3




Add the full path of pid2.txt in your script, it will then work
– Arushix
Jun 21 at 5:32





Add the full path of pid2.txt in your script, it will then work
– Arushix
Jun 21 at 5:32













Great, I will add it as the answer
– Arushix
Jun 21 at 5:42




Great, I will add it as the answer
– Arushix
Jun 21 at 5:42












I'm unfamiliar with the -e flag for pkill. What does it do on your system, and what Unix are you using?
– Kusalananda
Jun 21 at 6:02




I'm unfamiliar with the -e flag for pkill. What does it do on your system, and what Unix are you using?
– Kusalananda
Jun 21 at 6:02












Ubuntu 16.04. -e echoes which PID was killed.
– sjaak
Jun 21 at 7:02




Ubuntu 16.04. -e echoes which PID was killed.
– sjaak
Jun 21 at 7:02










2 Answers
2






active

oldest

votes

















up vote
1
down vote













The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt file.



You may also remove exit from the end of the script. It serves no purpose.






share|improve this answer






























    up vote
    0
    down vote













    Add the full path of pid2.txt in your script, it will then work



    Explanation:
    cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron






    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f451020%2fpkill-p-variable-not-working-when-running-sh-under-cron%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt file.



      You may also remove exit from the end of the script. It serves no purpose.






      share|improve this answer



























        up vote
        1
        down vote













        The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt file.



        You may also remove exit from the end of the script. It serves no purpose.






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt file.



          You may also remove exit from the end of the script. It serves no purpose.






          share|improve this answer















          The cron job is not necessarily running with the same working directory that you happen to have in the interactive session. This means that the file pid2.txt might not be available where you think it is. Change the script so that it uses an absolute path to the pid2.txt file.



          You may also remove exit from the end of the script. It serves no purpose.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 21 at 6:19


























          answered Jun 21 at 5:56









          Kusalananda

          101k13199312




          101k13199312






















              up vote
              0
              down vote













              Add the full path of pid2.txt in your script, it will then work



              Explanation:
              cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron






              share|improve this answer

























                up vote
                0
                down vote













                Add the full path of pid2.txt in your script, it will then work



                Explanation:
                cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Add the full path of pid2.txt in your script, it will then work



                  Explanation:
                  cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron






                  share|improve this answer













                  Add the full path of pid2.txt in your script, it will then work



                  Explanation:
                  cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. Hence you need to explicitly specify path of all files, scripts being called by cron







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 21 at 6:09









                  Arushix

                  9968




                  9968






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451020%2fpkill-p-variable-not-working-when-running-sh-under-cron%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?