LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   make commands keep running after leaving a terminal session or ssh session (https://www.linuxquestions.org/questions/programming-9/make-commands-keep-running-after-leaving-a-terminal-session-or-ssh-session-925911/)

Danny3031 01-27-2012 12:03 AM

make commands keep running after leaving a terminal session or ssh session
 
I am trying to write a c++ code where i can run 3 parallel jobs via multithreading and want them to be alive even if i kill the terminal..But when ever i kill the terminal,the job also gets killed.Can i know how to do so?
Thanks in advance.

ukiuki 01-27-2012 12:26 AM

Use the & after the program command.
example:
Code:

$conky &
that should send the program to background.

Regards

paulsm4 01-27-2012 12:28 AM

Hi -

To run a program in the background so you can exit the terminal, use "nohup":

Code:

nohup myprog &

Danny3031 01-27-2012 12:31 AM

Yeah i know about the & operator but even if i am using it my code is not working.If i kill the terminal,my application also exits.
Lets say m trying to launch xlogo.so even if am using command : xlogo &. Then if i terminate the execution, xlogo goes in background.thats ok.but if i kill my terminal,then the xlogo is also killed but i want it to be alive.. So is there any way out so that i can keep my xlogo thing alive??

Danny3031 01-27-2012 12:34 AM

Yeah am trying to use nohup only.
I am using it lyk nohup xlogo & but killing the terminal also kills the xlogo..

pyroscope 01-27-2012 12:54 AM

You have to use a daemonization double-fork WITHIN the application.

This is an example: https://github.com/jgshort/Narcissis...c_daemonizer.c

Use Google to find LOTS of similar snippets.

Danny3031 01-27-2012 01:02 AM

It was way too complicated for me to understand. Can you explain me out in simple code example?
Or else i might show you my code over here.You check it and tell me where i m committing a mistake.

Danny3031 01-27-2012 01:16 AM

Can somebody please reply to my question??

Danny3031 01-27-2012 01:19 AM

I am not able to continue running xlogo if i kill my terminal.Can anybody please tell me what am i missing in my code?

Danny3031 01-27-2012 02:52 AM

is there nobody who can help me over this??

Danny3031 01-27-2012 03:12 AM

?????

dwhitney67 01-27-2012 06:01 AM

Quote:

Originally Posted by Danny3031 (Post 4585568)
Yeah am trying to use nohup only.
I am using it lyk nohup xlogo & but killing the terminal also kills the xlogo..

How are you "killing" the terminal?

If I run xlogo in the background, from a terminal, and exit the terminal gracefully (ie. exit or ctrl-d), then it keeps running.

If I kill the terminal using the X widget, then xlogo exits too. However, if I run xlogo using nohup, then it keeps running.

The title of your post indicated that you may also want to run xlogo via an SSH session, and I presume to keep it running even after the session has terminated. I do not believe this is possible, since it is SSH that is managing the port-forwarding of data, not the X11 server.


P.S. A little advice... when asking for help on this forum, continually requesting immediate help may actually dissuade other members from helping you. Always be patient. Sometimes it may take a day for someone to respond to your query.

firstfire 01-27-2012 07:51 AM

Hi.

If I understand your problem correctly, you may be interested in screen and/or tmux.

theNbomr 01-27-2012 10:36 AM

Quote:

Originally Posted by Danny3031 (Post 4585597)
It was way too complicated for me to understand. Can you explain me out in simple code example?
Or else i might show you my code over here.You check it and tell me where i m committing a mistake.

You posted back only 8 minutes after the reply to your question. How much can you possibly have tried in such a brief time? You do have to put in a little effort.
--- rod.

War3zWad|0 01-28-2012 02:53 AM

I would have to agree with firstfire in regards to your question. I use screen every day at work to run multiple tasks and when I exit the machine after detaching the screen session the task continues to run until completion.

and example:
Code:

[warezwaldo@m0b1l3 ~]$ screen
[warezwaldo@m0b1l3 ~]$ top -cd1
top - 02:47:55 up 2 days, 14:43,  4 users,  load average: 0.19, 0.15, 0.06
Tasks: 171 total,  1 running, 170 sleeping,  0 stopped,  0 zombie
Cpu(s):  2.9%us,  2.4%sy,  0.0%ni, 49.8%id, 44.4%wa,  0.5%hi,  0.0%si,  0.0%st
Mem:  2804540k total,  1864472k used,  940068k free,  721824k buffers
Swap:  4996208k total,        0k used,  4996208k free,  739076k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                       
30686 warezwal  20  0  396m  90m  32m S    6  3.3  0:24.16 /usr/lib/firefox/firefox-bin http://www.linuxquestions.org/questions/showthread.php?threadid=925
 1971 root      20  0 76420  57m  12m S    1  2.1  19:45.02 /usr/bin/Xorg -br :0 vt7 -nolisten tcp -auth /var/lib/xdm/authdir/authfiles/A:0-L1bGYa         
 8243 warezwal  20  0  311m  65m  29m S    1  2.4  5:05.16 kwin -session 101bf13f16c11b000131344581900000112470000_1327481044_934435                     
30799 warezwal  20  0  133m  44m  19m S    1  1.6  0:00.48 kdeinit4: konsole [kdeinit]                                                                   
30822 warezwal  20  0  2504  988  732 R    1  0.0  0:00.02 top -cd1                                                                                       
    1 root      20  0  2196  720  612 S    0  0.0  0:02.38 init [5]                                                                                       
    2 root      20  0    0    0    0 S    0  0.0  0:00.00 [kthreadd]                                                                                     
    3 root      RT  0    0    0    0 S    0  0.0  0:00.02 [migration/0]                                                                                 
    4 root      20  0    0    0    0 S    0  0.0  0:01.54 [ksoftirqd/0]                                                                                 
    5 root      RT  0    0    0    0 S    0  0.0  0:00.00 [watchdog/0]                                                                                   
    6 root      RT  0    0    0    0 S    0  0.0  0:00.02 [migration/1]                                                                                 
    7 root      20  0    0    0    0 S    0  0.0  0:10.36 [ksoftirqd/1]                                                                                 
    8 root      RT  0    0    0    0 S    0  0.0  0:00.00 [watchdog/1]                                                                                   
    9 root      20  0    0    0    0 S    0  0.0  0:06.95 [events/0]                                                                                     
  10 root      20  0    0    0    0 S    0  0.0  0:21.28 [events/1]                                                                                     
  11 root      20  0    0    0    0 S    0  0.0  0:00.00 [netns]                                                                                       
  12 root      20  0    0    0    0 S    0  0.0  0:00.00 [async/mgr]                                                                                   
  13 root      20  0    0    0    0 S    0  0.0  0:00.00 [pm]                                                                                           
  14 root      20  0    0    0    0 S    0  0.0  0:00.16 [sync_supers]                                                                                 
  15 root      20  0    0    0    0 S    0  0.0  0:00.25 [bdi-default]                                                                                 
[detached]

[warezwaldo@m0b1l3 ~]$ screen -ls
There is a screen on:
        30812.pts-1.m0b1l3      (Detached)
1 Socket in /var/run/uscreens/S-warezwaldo.

To exit screen use the following key sequence: CNTL+A+D then to return to your screen session (if only 1) use screen -x you will see that the task continues to run even if you disconnect from ssh. To exit screen just type exit.

Danny3031 01-30-2012 12:55 AM

Thanks.It worked for me.

Danny3031 01-30-2012 02:45 AM

Quote:

Originally Posted by theNbomr (Post 4585983)
You posted back only 8 minutes after the reply to your question. How much can you possibly have tried in such a brief time? You do have to put in a little effort.
--- rod.

Hey Listen what ever piece of code you gave to me was all crap. It was like searching for gold in the mud. And you dare not try to act over smart with me. So plz buzz off!!

acid_kewpie 01-30-2012 02:57 AM

Quote:

Originally Posted by Danny3031 (Post 4588027)
Hey Listen what ever piece of code you gave to me was all crap. It was like searching for gold in the mud. And you dare not try to act over smart with me. So plz buzz off!!

Please don't talk like that to our valued members who are trying to help you. You were clearly *amazingly* impatient. Posts take time to be replied to, and this is a VOLUNTARY site, no one has ANY right to get an answer at all. You provide a very brief question and never expanded on it to any useful extent despite the useful and well intentioned pointers being given.

theNbomr 01-30-2012 11:29 AM

Quote:

Originally Posted by Danny3031 (Post 4588027)
It was like searching for gold in the mud.

So, where do think gold is found?
--- rod.


All times are GMT -5. The time now is 12:35 PM.