Is there a native way to keep a perl script running on a server(uninterrupted by ssh disconnections) without the use of tools like tmux and screen ? I am using Ubuntu.
You can try the POSIX command nohup
Most shells also come with disown
As far as a perl native solution, you can simply use the signal handling features of perl.
$SIG{HUP} = sub {
print "got SIGHUP\n";
};
screen is what you are looking for.
Related
Let me explain better. What is gonna happen if I run a command in Linux and before it's done and you could enter another command I close the terminal. Would it still do the command or not?
Generally, you must expect that closing your terminal will hangup your command. But fear not! Linux has a solution for that too!
To ensure that your command completes, use the nohup argument first. Simply place it before whatever you are trying to do:
nohup ./some_program
nohup ./do_a_thing -frx -file input_file.txt
nohup grep "something" giant_list_of_files/* > temp_file.txt
The nohup command stands for "no hangup" and it will ensure that the command you execute continues to run, even if you close your terminal.
It depends on the process and your environment (job control shell options, VNC, etc). But typically, no. The process will get a "hangup" signal (message) from the operating system, and upon receiving that, will quit.
The nohup command, for example, arranges for processes to ignore the hangup signal from the OS. There are many ways to achieve the same result.
I would say it will abort att the status you are in just before the session close.
If you want to be sure to complete the job, you will need to use the nohup command.
http://en.wikipedia.org/wiki/Nohup
Read about nohups and daemons (-d)...
A good link is [link]What's the difference between nohup and a daemon?
Worth look at screen command, Screen command offers the ability to detach a long running process (or program, or shell-script) from a session and then attach it back at a later time.
Is there a command line utility that I can use for executing X based applications that will detach my applications from the terminal so they aren't closed if the terminal is closed?
I guess such a app could be called something like gnome-run if it existed.
I have tried dtach, but it seems that you have to provide a socket file which is a bit clunky to type. I have also tried nohup, but found that also to be a bit clunky to type by the time std out and err are redirected to /dev/null.
I guess I'm looking for a simple program that will do something similar to this:
#!/bin/bash
nohup $1 > /dev/null 2>&1 &
Yes, there is a way to do it: first you need to run your GUI app and send it to background, then you (probably) want to detach it from Bash task management. For example if I wanted to run gedit this way:
gedit &
disown %1
After that you can close your terminal window and gedit will not be killed. Enjoy!
You already wrote your program, it is called a shell script and you give it the name you like and put it somewhere. Then you either add that directory to your $PATH or in your bashrc you set:
alias gnome-run=<path>/my-awesome-script.sh
Why waste earth's resources on a program?
If you want to run an application (say, gedit) as if it was run from the GUI, use:
xdg-open /usr/share/applications/gedit.desktop
See this answer on superuser.
#!/usr/bin/perl
$sim = "multiq";
`make SCHED=$sim`;
`script > scripter`;
`echo hi`;
print pack("c", 04);
~
This script hangs when script is called. Not sure how to get the perl script to keep running.
Note that backticks (‘‘) run a command and return its output. If you're going to ignore the output, use system as in
system("make SCHED=$sim") == 0 or die "$0: make exited " . ($? >> 8)
If you want to fire-and-forget a program (that is, start it in the background without worrying about when it completes), you can use
system("script >scripter &");
You're have to run that all in one child process if you want it to all interact. See the perlipc for various ways to handle that.
you might want to look at Expect to control an interactive session
backticks (‘‘) run a command and return its output.So you can store and manipulate it further. If you want to ignore the output, use system(). Also if you want to run any process in background use & in the prefix of the process. For example you wish to start Gimp using your script simply say
system("gimp &");
On Ubuntu I compiled sbcl 1.0.35 with threading. I can happily use sbcl from the command line and my hunchentoot website works with threading but when I logout it's gone. When I attempt to nohup sbcl
nohup ./src/runtime/sbcl --core output/sbcl.core
I get
(SB-IMPL::SIMPLE-STREAM-PERROR "couldn't read from ~S" # 9)
I've attempted various combinations of redirecting the standard input to /dev/null or a file and using the script command line option but I don't quite get what is going on.
How do I start sbcl from the command line on linux with nohup and keep my repl(website) running?
RESTAS web-framework ("REST Application Server") by Andrey Moskvitin contains code to properly daemonize SBCL instance. See http://github.com/archimag/restas/blob/master/contrib/restas-daemon.lisp. You can easily rip off RESTAS-specific parts.
You could start SBCL in Gnu Screen and then detach from the session.
This also gives you the possibility to reattach to your REPL later.
Dmity-vk sent me on the right track, thank you. SBCL tries to start a repl when you start it up, and reads from the standard in. When you use nohup the standard in is redirected and cannot be read. SBCL then drops to the debugger which tries to read from standard in ... hence the endless loop. The --script tag comes close to solving this except that it has a (quit) once the script has been read. So I put an endless loop in my script and voila.
so on an ubuntu server with sbcl this should let you start a hunchentoot server
sudo nohup ./run-sbcl.sh --script foo.lisp > /dev/null 2> /dev/null &
where foo.lisp has as its last lines something like
(defvar *alive* t)
(loop (sleep 1000) (if (not *alive*) (quit)))
Let's take example of a command "example-command".
I open terminal
I write example-command in terminal, and example-command executes.
Now if I close terminal, example-command gets killed too.
I now try with "example-command &", but the same behaviour.
How do I execute a command so that when I close the terminal, the command doesn't get terminated?
There are two ways, identical in result.
Use nohup when you start your program. E.g., nohup example-command. You can background and work with it normally; it will simply continue running after you've quit.
Alternatively, as #alamar noted, if you use bash as your shell, you can us the disown command. Unfortunately, as far as I know, disown is bash-specific; if you use another shell, such tcsh, you may be restricted to the nohup form above.
Please search for similar questions first.
Besides the ways listed above, you can do:
setsid command_name
For example:
setsid xclock
Thanks
In Zsh (not bash) you can:
example-command &; disown {pid}
or just
example-command &; disown
You could also consider using the screen command.
nohup example-command
You can also use the 'at' or 'batch' commands and give it the current time.
disown is a bash builtin. You could create a wrapper shellscript for your command such as
#!/bin/bash
$1 &
P=`which $1`
disown `pidof ${P}`
Not the most robust script (by any means) but may help get you going. For example:
$./launch_script.sh myProgram
You can also do this in the source of the program if you are editing it.
Run: example-command
Press: Control-Z
Run: bg