Using session command from a cron job (linux)

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











up vote
0
down vote

favorite












I need to be able to start a named "session" from within a cron job and run a job within that named session. The job I need to run MAY cause my appliance to not run properly if problem exist and I need to be able to reattach to the named session created if needed/to close it later. Does anyone have any idea how I might do this?







share|improve this question


























    up vote
    0
    down vote

    favorite












    I need to be able to start a named "session" from within a cron job and run a job within that named session. The job I need to run MAY cause my appliance to not run properly if problem exist and I need to be able to reattach to the named session created if needed/to close it later. Does anyone have any idea how I might do this?







    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need to be able to start a named "session" from within a cron job and run a job within that named session. The job I need to run MAY cause my appliance to not run properly if problem exist and I need to be able to reattach to the named session created if needed/to close it later. Does anyone have any idea how I might do this?







      share|improve this question














      I need to be able to start a named "session" from within a cron job and run a job within that named session. The job I need to run MAY cause my appliance to not run properly if problem exist and I need to be able to reattach to the named session created if needed/to close it later. Does anyone have any idea how I might do this?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 6 '17 at 19:26

























      asked Nov 6 '17 at 19:14









      Marshal Tucker

      31




      31




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          GNU Screen or TMUX are probably your best options. The general concept of both is pretty similar to a tabbed window manager, but they're both for terminal usage, and you can detach from a session and re-attach later. I'm not quite certain about the syntax needed for TMUX, but for screen the command you want is:



          screen -D -n -s <name> <command>


          Replace <name> with the name of the session, and <command> with the command to run. YOu can then re-attach to the session with:



          screen -D -r <name>


          The only caveat is that yuu have to be running as the same user when you try to reattach that the session was started as (you can technically reattach to other users sessions, but it's a bit more complicated).






          share|improve this answer




















          • hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
            – Marshal Tucker
            Nov 6 '17 at 19:59







          • 1




            Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
            – DopeGhoti
            Nov 6 '17 at 20:11










          • You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
            – Austin Hemmelgarn
            Nov 6 '17 at 20:19










          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%2f402900%2fusing-session-command-from-a-cron-job-linux%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          GNU Screen or TMUX are probably your best options. The general concept of both is pretty similar to a tabbed window manager, but they're both for terminal usage, and you can detach from a session and re-attach later. I'm not quite certain about the syntax needed for TMUX, but for screen the command you want is:



          screen -D -n -s <name> <command>


          Replace <name> with the name of the session, and <command> with the command to run. YOu can then re-attach to the session with:



          screen -D -r <name>


          The only caveat is that yuu have to be running as the same user when you try to reattach that the session was started as (you can technically reattach to other users sessions, but it's a bit more complicated).






          share|improve this answer




















          • hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
            – Marshal Tucker
            Nov 6 '17 at 19:59







          • 1




            Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
            – DopeGhoti
            Nov 6 '17 at 20:11










          • You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
            – Austin Hemmelgarn
            Nov 6 '17 at 20:19














          up vote
          1
          down vote



          accepted










          GNU Screen or TMUX are probably your best options. The general concept of both is pretty similar to a tabbed window manager, but they're both for terminal usage, and you can detach from a session and re-attach later. I'm not quite certain about the syntax needed for TMUX, but for screen the command you want is:



          screen -D -n -s <name> <command>


          Replace <name> with the name of the session, and <command> with the command to run. YOu can then re-attach to the session with:



          screen -D -r <name>


          The only caveat is that yuu have to be running as the same user when you try to reattach that the session was started as (you can technically reattach to other users sessions, but it's a bit more complicated).






          share|improve this answer




















          • hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
            – Marshal Tucker
            Nov 6 '17 at 19:59







          • 1




            Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
            – DopeGhoti
            Nov 6 '17 at 20:11










          • You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
            – Austin Hemmelgarn
            Nov 6 '17 at 20:19












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          GNU Screen or TMUX are probably your best options. The general concept of both is pretty similar to a tabbed window manager, but they're both for terminal usage, and you can detach from a session and re-attach later. I'm not quite certain about the syntax needed for TMUX, but for screen the command you want is:



          screen -D -n -s <name> <command>


          Replace <name> with the name of the session, and <command> with the command to run. YOu can then re-attach to the session with:



          screen -D -r <name>


          The only caveat is that yuu have to be running as the same user when you try to reattach that the session was started as (you can technically reattach to other users sessions, but it's a bit more complicated).






          share|improve this answer












          GNU Screen or TMUX are probably your best options. The general concept of both is pretty similar to a tabbed window manager, but they're both for terminal usage, and you can detach from a session and re-attach later. I'm not quite certain about the syntax needed for TMUX, but for screen the command you want is:



          screen -D -n -s <name> <command>


          Replace <name> with the name of the session, and <command> with the command to run. YOu can then re-attach to the session with:



          screen -D -r <name>


          The only caveat is that yuu have to be running as the same user when you try to reattach that the session was started as (you can technically reattach to other users sessions, but it's a bit more complicated).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 6 '17 at 19:54









          Austin Hemmelgarn

          5,1741915




          5,1741915











          • hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
            – Marshal Tucker
            Nov 6 '17 at 19:59







          • 1




            Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
            – DopeGhoti
            Nov 6 '17 at 20:11










          • You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
            – Austin Hemmelgarn
            Nov 6 '17 at 20:19
















          • hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
            – Marshal Tucker
            Nov 6 '17 at 19:59







          • 1




            Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
            – DopeGhoti
            Nov 6 '17 at 20:11










          • You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
            – Austin Hemmelgarn
            Nov 6 '17 at 20:19















          hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
          – Marshal Tucker
          Nov 6 '17 at 19:59





          hmmmm, so if it runs in a cron job it will not run as "me" so when I reattach I'd have to do the "more complicated" method. Thanks for the help, I'll test. You don't want to leave some more complicated help with the reattach do you?
          – Marshal Tucker
          Nov 6 '17 at 19:59





          1




          1




          Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
          – DopeGhoti
          Nov 6 '17 at 20:11




          Simply log in as the user who owns the cron job and run the same command to reattach. (n. b. I usually use screen -x rather than screen -r as I often use multiclient sessions and -x is a superset of -r in that it will not fail if there is still a screen client attached.)
          – DopeGhoti
          Nov 6 '17 at 20:11












          You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
          – Austin Hemmelgarn
          Nov 6 '17 at 20:19




          You don't even have to log in technically, you can also use a tool like sudo to switch to the appropriate UID (though I'm pretty sure su won't work for this because it doesn't reset the environment properly). If you absolutely can't run screen -r or screen -x as that user, you can also prefix the session name with that user's name followed by a /, but I've had issues in the past with that not working reliably.
          – Austin Hemmelgarn
          Nov 6 '17 at 20:19

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402900%2fusing-session-command-from-a-cron-job-linux%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)