How to Stop & Start Screen Jobs Using Scripts in Linux - linux

I want to use some scripts to stop and start a bunch of programs, each running in a separate linux screen. These programs run continuously and need to be stopped using Ctrl-C.
So I can write some code to stop a screen:
screen -S "mysessionname" -X quit
but do I need to send a Ctrl-C somehow first of all and if so then how?
Also, I can start a new detached screen thus from within a script:
screen -mdS "mysesssionname"
but how can I then kick of the job from within this screen using a script? I've tried attaching to the session and then starting the job all from within a script but it doesn't seem to work

Well let's hope that this can help you but to simulate the Ctrl-C in a script as far as i know you can use something like kill -3 and the pid of the process. See in manual of signals: "man 7 signal"

Related

After disown a process how can I again read the stdout of the running process

I have a c program which is running on linux. I applied some command for running the process in background after closing the terminal.
1. Pause program with `Ctrl-Z`,
2. Pull it into the background with `bg`
3. Then disown it.
Now i want to see the console output of this program which is still running.
How can I do it in linux?
If the terminal used to run the command has been closed and you are using a new one, I guess that the standard output of the background process was closed, so that you cannot do that.
A very simple way is to use screen.
Another way is to redirect output to a file when the command is run, and access the file later.

Move process to other TTY

Is it possible to move a process from one terminal to another in Linux? For example, if I have executed the "top" command via a gnome-terminal window, I want to move the output shown in the gnome-terminal window to a TTY terminal.
It's too late by the time you've already executed it, but if you recognise in advance that you might want to move it, you can use
screen top
to run top, and then detach from the running process with Ctrl-A and D. After that, you can run
screen -r
from any terminal on the same machine to reattach.
It's very powerful, and does a lot more than just that.

What does the linux make command do?

I created a database and a table inside it.I insert records manually and then run a python script which reads the records and updates them in the table. I was just trying
the make &>1.txt command to redirect the output when the unexpected happened.
Even when I am not running the script and insert the record manually from mysql, the script is
running somewhere as it updates the records , but I am not running it any tab
What is the reason ??
The script just starts running by itself!!!
If it's running in the background (which could have happened if you typed make & >1.txt with a space, not what you showed above) then the jobs command will show it is running, and fg will bring it to the foregound, so you can kill it with Ctrl-C
(You can also kill it with the jobspec shown by jobs e.g. kill %1 but if you don't know what you're doing it's simpler to bring it to the foreground and interrupt it)

Running a node.js process in background after giving inputs to it

I have this node.js server which, once spawned, expects some input from stdin. The inputs shouldn't be given straight away: I need to wait for some event before giving them (e.g. a connection from somebody). If I give the commands and close the shell, the server shuts itself down. I would like to give the input to the server and close my shell (effectively leaving the server running).
I know that to run a process in background I need to do for example node my_server.js &, but this prevents the input from the command line. I would like to give this input AND then put it in background. Modules like forever puts it in the background automatically without letting me giving the inputs through stdin.
Moreover putting the script in background kills anyways the server when closing the shell.
Any suggestion?
Thanks
I did a quick test just using gedit in Ubuntu 12.04, and it worked.
Start your node app like so "node app.js arg1 arg2" however you want to and hit enter to start the program. Then hit CTRL-z once your program has started running. This gives you the terminal back but stops the process in the background. To let it run in the background now, simply "bg" and hit enter. This will let the process keep running now but in the background.
You can confirm you are still up with the command "ps -ef | grep node" which should show your program still running.
However, this does still leave the node process attached to the terminal window so when you close the terminal window it will close the process. But I think this will get you most of what you seem to be looking for quick and easy.
You asked for any suggestion, so here it is: make your server able to start without user interaction. The simplest way to do it is probably to create a file containing exactly the input needed by the server, then starting it like this:
node my_server.js < my_input.txt &
If the input needed depends on what the server outputs (ouch), use expect(1). If possible, subvert the whole thing and use a module like commander to get your inputs from the command line instead of stdin.

Run a command in a shell and keep running the command when you close the session

I am using Putty to connect to a remote server. What I want to know is if there is any way to write my commands and allow them to keep running after I close the session with Putty. The reason for this is that I do not want to keep the computer ON all the time. Is there any way to do this?.
Update with the solution
For my question as it is presented the best solution is use one of the commands provided such as nohup, because you do not have to install any additional software. But if you are in the same problem use screen, install it and use it. It is amazing.
I have selected the answer of Norman Ramsey as favourite because propose several solutions using commands and screen. But please check the other answers specially the one of PEZ, then you get an insight of what screen is able todo.
screen! It's the best thing since sliced bread. (Yeah, I know others have already suggested it, but it's so good the whole world should join in and suggest it too.)
screen is like, like, ummmm ... like using VNC or the like to connect to a GUI destop, but for command shell windows. You can have several shell "windows" open at once in the same screen session. You can do stuff like:
Start a screens session using "screen -dR" (get used to using -dR)
run some commands in one window
press CTRL-A,C to create a new window open a file there in vim
press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
CTRL-A, 1 to go back to your vim session
CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
CTRL-A, 0 and start a background process
CTRL-A, C to create yet a new window, "tail -f" the log for that background process
CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
Go on vacation for three weeks
Log on to the server again and issue "screen -dR" to connect to your existing screen session
check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
CTRL-A, 1 to pick up that vim session again
I guess you're starting to get the picture now? =)
It's like magic. I've been using screen for longer than I can remember and I'm still totally amazed with how bloody great it is.
EDIT: Just want to mention there's now also tmux. Very much like screen, but has some unique features, splitting the windows being the most prominent one.
nohup, disown, and screen are all good but screen is the best because unlike the other two, screen allows you to disconnect from the remote server, keep everything running, and then reconnect later to see what is happening. With nohup and disown you can't resume interacting.
Try using GNU Screen. It allows you to have several shells open at once. And you can disconnect from those running shells (i.e. close session with Putty) and they will keep doing their thing.
What you are looking for is nohup.
See the wiki link for how to use it.
screen is the best.
Try:
screen -dmS "MyTail" tail -f /var/log/syslog
This start command in background.
Use screen -r to list, and or screen -r Mytail to enter session.
If more users need access same session, use: screen -rx MyTail, and both or more users share the session.
If you can't use screen (because, for instance, your SSH session is being programmatically driven), you can also use daemonize to run the program as a daemon.
One way that works well for me is at.
at works like cron, but for a one-time job. I used it today to download a large file without having to keep my session alive.
for example:
$ at 23:55
at> wget http://file.to.download.com/bigfile.iso
at> ^D
You pass at a time (in the future) and it gives you a prompt. You enter the commands you want to run at that time and hit ctrl+d. You can exit out of your session and it will run the commands at the specified time.
Wikipedia has more info on at.
./command & disown
ssh localhost && ./command && exit

Resources