If GNU screen already exists, reattach to it, else create it

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











up vote
0
down vote

favorite
1












I'm trying to do this: if a GNU screen named worker already exists, then reattach to it, else create it by starting python example.py.



I tried:



if [ls /var/run/screen/S-root/ |grep -Fxq worker] then screen -r worker else cd /home/www/example/; screen -S worker python example.py fi


but it doesn't seem to work.



Is there something wrong in the syntax?







share|improve this question



















  • What do you mean by this screen -S worker python example.py, you want to create a session and run python example.py in it ?
    – Bharat
    Apr 18 at 20:19










  • @Bharat yes, if the session is not already running and detached.
    – Basj
    Apr 18 at 20:20














up vote
0
down vote

favorite
1












I'm trying to do this: if a GNU screen named worker already exists, then reattach to it, else create it by starting python example.py.



I tried:



if [ls /var/run/screen/S-root/ |grep -Fxq worker] then screen -r worker else cd /home/www/example/; screen -S worker python example.py fi


but it doesn't seem to work.



Is there something wrong in the syntax?







share|improve this question



















  • What do you mean by this screen -S worker python example.py, you want to create a session and run python example.py in it ?
    – Bharat
    Apr 18 at 20:19










  • @Bharat yes, if the session is not already running and detached.
    – Basj
    Apr 18 at 20:20












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I'm trying to do this: if a GNU screen named worker already exists, then reattach to it, else create it by starting python example.py.



I tried:



if [ls /var/run/screen/S-root/ |grep -Fxq worker] then screen -r worker else cd /home/www/example/; screen -S worker python example.py fi


but it doesn't seem to work.



Is there something wrong in the syntax?







share|improve this question











I'm trying to do this: if a GNU screen named worker already exists, then reattach to it, else create it by starting python example.py.



I tried:



if [ls /var/run/screen/S-root/ |grep -Fxq worker] then screen -r worker else cd /home/www/example/; screen -S worker python example.py fi


but it doesn't seem to work.



Is there something wrong in the syntax?









share|improve this question










share|improve this question




share|improve this question









asked Apr 18 at 19:58









Basj

6031731




6031731











  • What do you mean by this screen -S worker python example.py, you want to create a session and run python example.py in it ?
    – Bharat
    Apr 18 at 20:19










  • @Bharat yes, if the session is not already running and detached.
    – Basj
    Apr 18 at 20:20
















  • What do you mean by this screen -S worker python example.py, you want to create a session and run python example.py in it ?
    – Bharat
    Apr 18 at 20:19










  • @Bharat yes, if the session is not already running and detached.
    – Basj
    Apr 18 at 20:20















What do you mean by this screen -S worker python example.py, you want to create a session and run python example.py in it ?
– Bharat
Apr 18 at 20:19




What do you mean by this screen -S worker python example.py, you want to create a session and run python example.py in it ?
– Bharat
Apr 18 at 20:19












@Bharat yes, if the session is not already running and detached.
– Basj
Apr 18 at 20:20




@Bharat yes, if the session is not already running and detached.
– Basj
Apr 18 at 20:20










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










It makes more sense to use screen -ls or



screen -S worker -x || cd /home/www/example; screen -S worker python example.py; 


But the errors in your code are




  1. the unnecessary [ (which would have needed spaces around it)



    if [ls /var/run/screen/S-root/ |grep -Fxq worker]


    must be



    if ls /var/run/screen/S-root/ | grep -Fxq worker



  2. the missing ; / newline before then



    |grep -Fxq worker] then


    must be



    | grep -Fxq worker; then



  3. the missing ; / newline before else



     then screen -r worker else


    must be



     then screen -r worker; else



  4. the missing ; / newline before fi



    python example.py fi


    must be



    python example.py; fi






share|improve this answer





















  • Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
    – Basj
    Apr 18 at 20:37






  • 1




    @Basj I use that every day because I start screen as root with the config file of my normal user account.
    – Hauke Laging
    Apr 18 at 20:41










  • Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
    – Basj
    Apr 18 at 20:42










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%2f438585%2fif-gnu-screen-already-exists-reattach-to-it-else-create-it%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
2
down vote



accepted










It makes more sense to use screen -ls or



screen -S worker -x || cd /home/www/example; screen -S worker python example.py; 


But the errors in your code are




  1. the unnecessary [ (which would have needed spaces around it)



    if [ls /var/run/screen/S-root/ |grep -Fxq worker]


    must be



    if ls /var/run/screen/S-root/ | grep -Fxq worker



  2. the missing ; / newline before then



    |grep -Fxq worker] then


    must be



    | grep -Fxq worker; then



  3. the missing ; / newline before else



     then screen -r worker else


    must be



     then screen -r worker; else



  4. the missing ; / newline before fi



    python example.py fi


    must be



    python example.py; fi






share|improve this answer





















  • Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
    – Basj
    Apr 18 at 20:37






  • 1




    @Basj I use that every day because I start screen as root with the config file of my normal user account.
    – Hauke Laging
    Apr 18 at 20:41










  • Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
    – Basj
    Apr 18 at 20:42














up vote
2
down vote



accepted










It makes more sense to use screen -ls or



screen -S worker -x || cd /home/www/example; screen -S worker python example.py; 


But the errors in your code are




  1. the unnecessary [ (which would have needed spaces around it)



    if [ls /var/run/screen/S-root/ |grep -Fxq worker]


    must be



    if ls /var/run/screen/S-root/ | grep -Fxq worker



  2. the missing ; / newline before then



    |grep -Fxq worker] then


    must be



    | grep -Fxq worker; then



  3. the missing ; / newline before else



     then screen -r worker else


    must be



     then screen -r worker; else



  4. the missing ; / newline before fi



    python example.py fi


    must be



    python example.py; fi






share|improve this answer





















  • Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
    – Basj
    Apr 18 at 20:37






  • 1




    @Basj I use that every day because I start screen as root with the config file of my normal user account.
    – Hauke Laging
    Apr 18 at 20:41










  • Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
    – Basj
    Apr 18 at 20:42












up vote
2
down vote



accepted







up vote
2
down vote



accepted






It makes more sense to use screen -ls or



screen -S worker -x || cd /home/www/example; screen -S worker python example.py; 


But the errors in your code are




  1. the unnecessary [ (which would have needed spaces around it)



    if [ls /var/run/screen/S-root/ |grep -Fxq worker]


    must be



    if ls /var/run/screen/S-root/ | grep -Fxq worker



  2. the missing ; / newline before then



    |grep -Fxq worker] then


    must be



    | grep -Fxq worker; then



  3. the missing ; / newline before else



     then screen -r worker else


    must be



     then screen -r worker; else



  4. the missing ; / newline before fi



    python example.py fi


    must be



    python example.py; fi






share|improve this answer













It makes more sense to use screen -ls or



screen -S worker -x || cd /home/www/example; screen -S worker python example.py; 


But the errors in your code are




  1. the unnecessary [ (which would have needed spaces around it)



    if [ls /var/run/screen/S-root/ |grep -Fxq worker]


    must be



    if ls /var/run/screen/S-root/ | grep -Fxq worker



  2. the missing ; / newline before then



    |grep -Fxq worker] then


    must be



    | grep -Fxq worker; then



  3. the missing ; / newline before else



     then screen -r worker else


    must be



     then screen -r worker; else



  4. the missing ; / newline before fi



    python example.py fi


    must be



    python example.py; fi







share|improve this answer













share|improve this answer



share|improve this answer











answered Apr 18 at 20:25









Hauke Laging

53.2k1282130




53.2k1282130











  • Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
    – Basj
    Apr 18 at 20:37






  • 1




    @Basj I use that every day because I start screen as root with the config file of my normal user account.
    – Hauke Laging
    Apr 18 at 20:41










  • Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
    – Basj
    Apr 18 at 20:42
















  • Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
    – Basj
    Apr 18 at 20:37






  • 1




    @Basj I use that every day because I start screen as root with the config file of my normal user account.
    – Hauke Laging
    Apr 18 at 20:41










  • Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
    – Basj
    Apr 18 at 20:42















Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
– Basj
Apr 18 at 20:37




Thanks a lot, your solution screen -S worker -x || ... works fine. And I learnt about the missing ; + unncessary [, thanks as well!
– Basj
Apr 18 at 20:37




1




1




@Basj I use that every day because I start screen as root with the config file of my normal user account.
– Hauke Laging
Apr 18 at 20:41




@Basj I use that every day because I start screen as root with the config file of my normal user account.
– Hauke Laging
Apr 18 at 20:41












Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
– Basj
Apr 18 at 20:42




Also my -Fxq was wrong. if screen -ls |grep -Fq worker; then screen -r worker; else cd /home/www/example/; screen -S worker python example.py; fi is correct.
– Basj
Apr 18 at 20:42












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438585%2fif-gnu-screen-already-exists-reattach-to-it-else-create-it%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?