PuTTy background processing executing IDL scripts - linux

I'd like to start an extensive IDL process which will be run on a linux based server.
Given a normal a procedure like this:
open PuTTy
move to right directory
compile IDL scripts (1 main script executing 2 others)
run main script.
Now I would like to do that in the background. After some Google research I found out that it is adivisable to use the nohup-command. But I don't know what this command should look like including my scripts....
Thanks for your help.

nohup command can be used to run any job in background which you run in foreground.
The syntax for it is :
nohup your_script &
You can use the fg to bring this job to foreground.

To detach your current process you can use two solutions:
nohup
Run:
nohup ./process </dev/null &>log.txt &
disown to detach the process from your pseudo-TTY, so that when you exit, the process doesn't terminate.
screen
With nohup you lose all control over the process and all you can do is to kill it. It might be a better idea to use screen which lets you gain access to the detached "TTY" again:
Run screen; screen will spawn a new shell.
Run ./process normally
Now hit Ctrl+A and then D - you will get back to your parent shell
You can logout now
After you login again, you can view the task again with screen -r

Related

Google Cloud Shell scripting Issue

I am running my code from the shell(SSH) in google cloud as
python3 mycode.py
If I close the shell, the computation stops. How can I start a computation and then close the shell(Computation takes a long Time:)).....come back later and see how it is doing.
My code keeps printing results after a certain number of iteration.
Well, in general what you can do is run the code in a way where you can detach from the interactive environment. Using a tool such as screen or tmux. However, Google Cloud Shell is not made for running background tasks, and if i recall correctly, it will terminate after an hour.
You might need to provision a virtual machine to run it on instead. I can recommend using tmux. With tmux, it will be as simple as running tmux and then in the new shell running your script python3 mycode.py. You can then detach using ctrl+b d or simply disconnect. When you reconnect you run tmux attach -dto get back to your script.
I will provide you as well the following possibility:
A Bash Builtin command that you could also use is disown. First, run your process/script in the background (using &, or stopping it with ^Z and then restarting with bg):
$ long_operation_command &
[1] 1156
Note that at this point the process is still linked to the session and in case it is closed it will be killed.
You can the process attached to the session check running jobs in the background:
$ jobs
[1]+ Running long_operation_command
Therefore you can run disown in order to detach the processes from the session:
$ disown
You can confirm this checking the result of your script or command logging in again or checking with top the process still running.
Check also this because it could be interesting, i.e. the difference between nohup foo, foo & and $ foo & disown

Start Bash Script from Bash Script to Launch GUI Application

I am trying to launch a GUI application (rhythmbox) on a Ubuntu. In the following I try to explain the chain of executed files.
# Window manager executes first
~/i3wm_cmd_wrapper.sh Window_Name ~/mount_enc.sh
This wrapper uses gnome-terminal to execute stuff. This enables opening a terminal at startup where users can enter information.
# mount_enc.sh launches the following command in the end
bash ~/launch_in_bg.sh rhythmbox
mount_enc.sh does exactly what it is supposed to do when starting from a normal terminal. But I'd like to start it automatically at startup and rhythmbox should be kept open after the script is done.
# launch_in_bg.sh is just doing what it's supposed to
($PRGRM > /dev/null 2>&1) &
I can not get the gnome-terminal to open rhythmbox for me. Also I think my approach is wrong if I want rhythmbox to keep running after the gnome-terminal finishes executing the mount_enc.sh script. Can anybody think of a better solution?
If you open a program from the console (even in background), the process of the program is a child process of the console process and will terminate when the console process terminates.
To keep the program's process running it has to be detached from the console process. Detaching can be done in multiple ways. Some examples:
nohup rhythmbox &
or
rhythmbox & disown
To suppress output, use redirection as in your script.

How can I run a Linux command that still runs after I close my PuTTY SSH session?

I am connecting to my NAS over putty which is running linux on it.
I wanted to move a big directory from one location to another. Is it possible to keep the process running after I close the putty session ?
I am afraid that if I close putty the files will not be copied to the end ?
Start your task with 'nohup' and put it in the background with '&', e.g.:
$ nohup mv /here /there &
$ exit
and it should continue running.
I would suggest using screen for this.
Start a new screen,
screen -S <name of your screen>
and then you can perform your commands there, detach from the screen and re-attach to it at any time.
Detach by hitting the sequence
ctrl a d
and re-attach by typing
screen -r (or list the screens with screen -l).
Also have a look at Gnu screen survival guide.
You can run it as a background process as follows:
nohup mv source target &
However, you will not be able to interact with the process.
EDIT: nohup is also required to keep it running after the shell exits.
Using nohup would be best solution.
The following command in your terminal may help you out to run the script using nohup and redirect the output in your desired file.
General Syntax
nohup some_command &> nohup_log_file_name.out &
Example
nohup python script.py &> nohup_log_script.out &
So, if you use the above command, you will be able to find the outputs of the command in a log file named nohup_log_script.out

how can I continously run a unix script in background without using crontab.

how can I continously run a script in background without using crontab. The script should run even after I logout and is acceptable if it doesen't start after system reboot.I am new to unix.
There's a couple of ways of doing this but the neatest way is to use screen. This lets you create a session which lives perminently on the machine and you can simply reconnect to to check on the progress of your long running process.
If you don't wish to use screen you can use nohup this allows you to run a task like:
nohup mytask &
Your task will now run in the background and will survive a log off, however there's no way to take control of it again, unlike with screen.
if [ "x$1" != "x--" ]; then
$0 -- 1> /dev/null 2> /dev/null &
exit 0
fi
This is how you can run a script as a daemon. First your script (the father) will create a copy of himself (a child) so it is considerd as a process of the father. Then the father kills itself while the child is still running. Guess what happens when you do such a thing ? The child is attached to the init process. So even if you logout, the script will still run.
You can even start it without the "&" operator because you start the father which is killed a millisecond after.
You can take control over it again like any program running on your computer.
By the way it's not a real "daemon" program, it's just kind of emulation. You can't just start it at the boot (I mean really the BOOT and not the loggin) if you want to start it as you login, quite simple put it in your .xinitrc
The main advantage of this solution is that your script doesn't depend on any other programm such as "nohup" which is really bad I think.
Regards
PS : If you want some informations about what the command above does, just ask me. It's just a "parameter" thing.
As others have mentioned before, you need to use nohup to prevent the process from getting the hangup signal (hence no-h-up).
However, if you start the process in the background to begin with, as
prompt> nohup process &
that has the disadvantage of not allowing you to enter any data that may be required to get the process started off. This may be passwords/credentials or other input the process needs.
If you have that requirement, start it without the "&" at the end, enter your input and then hit Ctrl-Z to put the process to sleep. To send it to the background, type "bg" at the prompt and hit Enter.
prompt> nohup process
Enter password:
(Now press Ctrl-Z)
[1]+ Stopped process
prompt> bg
[1]+ process &
Now even if you log off, the process will continue to run in the background.
Alternatively if you are using bash or zsh, if you didn't start the process with nohup to begin with, and killing it and restarting is not an option then you can use the built-in disown command. First pause and background the process. And then stop hangup signals from reaching it.
prompt> process
Enter password:
(Now press Ctrl-Z)
[1]+ Stopped process
prompt> bg
[1]+ process &
prompt> disown -h
Note: If you've got other background jobs running, you need to provide the jobspec to only disown this specific job.
prompt> disown -h %1
Instead of [1] if you'd seen [2] when you paused and sent the process to the background, you'd say disown -h %2 instead.
As well as starting it in the background, as above, you may need to use 'nohup'. This means it will carry on running, even if you close the terminal.
nohup ./abc.sh &
start it in background using & operator e.g.
./abc.sh & this will continue till (a) the execution is complete or (b) you kill it or (c) system reboots

Asynchronous shell commands

I'm trying to use a shell script to start a command. I don't care if/when/how/why it finishes. I want the process to start and run, but I want to be able to get back to my shell immediately...
You can just run the script in the background:
$ myscript &
Note that this is different from putting the & inside your script, which probably won't do what you want.
Everyone just forgot disown. So here is a summary:
& puts the job in the background.
Makes it block on attempting to read input, and
Makes the shell not wait for its completion.
disown removes the process from the shell's job control, but it still leaves it connected to the terminal.
One of the results is that the shell won't send it a SIGHUP(If the shell receives a SIGHUP, it also sends a SIGHUP to the process, which normally causes the process to terminate).
And obviously, it can only be applied to background jobs(because you cannot enter it when a foreground job is running).
nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.
The process won't receive any sent SIGHUP.
Its completely independent from job control and could in principle be used also for foreground jobs(although that's not very useful).
Usually used with &(as a background job).
nohup cmd
doesn't hangup when you close the terminal. output by default goes to nohup.out
You can combine this with backgrounding,
nohup cmd &
and get rid of the output,
nohup cmd > /dev/null 2>&1 &
you can also disown a command. type cmd, Ctrl-Z, bg, disown
Alternatively, after you got the program running, you can hit Ctrl-Z which stops your program and then type
bg
which puts your last stopped program in the background. (Useful if your started something without '&' and still want it in the backgroung without restarting it)
screen -m -d $command$ starts the command in a detached session. You can use screen -r to attach to the started session. It is a wonderful tool, extremely useful also for remote sessions. Read more at man screen.

Resources