How to keep process backgrounded after script is done? - shell

I'm trying to background a process in a zsh script:
lein servlet run &
but after the script is done jobs shows:
[4] + 98346 suspended (tty input) lein servlet run
If I run lein servlet run & from my command line, it works fine, and doesn't get suspended.

After researching, I learned that (tty input) means the process is waiting for terminal input, but it doesn't actually take input from me (someone must've wrote something hacky). To solve the problem, I gave it fake input:
lein servlet run </dev/zero &
Now it works. :D

Related

How do I handle stopping my service?

I've turned a program I wrote into a service, and I have a bash script that runs to start up the actual program, since there are things that have to be started in a particular order. The contents of the startup script (called with start-stop-daemon from the init.d script look like :
./rfid_reader &
sleep 2
java ReaderClass &
This works fine, but how do I go about killing them when it comes time to stop the process?
I've seen pidfiles used, do I just get the PIDs of the two programs, write them to a file, and then kill them when it comes time to shut them down, or do I have to ps -ef | grep program to get their PIDs?
I don't think killall is a good idea to do this. You'd better to record the PID of the program started in background in some file(e.g. PID_FILE) and then kill $(<$PID_FILE) to stop it.
Please refer to this thread for how to get the PID the previous started background program.
Assuming you know the name of your program, you can kill them as below :
killall -KILL program_name

How to run a script in background in Linux. - shell scripting

A script that keeps updating the log file. data like system time and date, users currently logged in etc for every interval of time say 5 minutes. THE SCRIPT MUST RUN EVEN AFTER THE TERMINAL HAS BEEN CLOSED.
Actually, no.
First of, you don't need sh:
$ ./newscript.sh &
This is enough. This will start a background process. But your terminal is still controlling it. To achieve the behavior you want, do this:
$ disown %1
This will disown the job with the jobspec 1 (which is like an id), which was the one you started beforehand. Now you can close the terminal.
Hurrah!! I would like to answer my question since i have got the solution.
For example, I'm running a script newscript.sh I want to run this in background and continue someother job in the terminal or i can close the terminal.
[yourname # username ~]$ sh newscript.sh &
and hit enter. You will get a PID and your job will be attached to the background.
To kill the same process, use the PID
For eg.,
kill 1205212
Thank you.

exit console after starting play framework application

I write a shell script for starting two play framework application.
cd /someDirectory
activator "start -Dhttp.port=9001"
cd /anotherDirectory
activator "start -Dhttp.port=9002"
after running the shell script, running process sleeps after starting first application (because play framework stay in shell after start command until pressing Ctrl + D)
How can I say to activator release shell after starting?
Use any process manager you want :
screen
nohup
upstart
systemd
supervisord
BTW, if this is production, you might consider using the start script created when packaging your application.
Use nohupas follows nohup ./script &. It is also useful when starting applications from remote shell.

how I can start lein ring server in background?

Now what I do is:
$ lein ring server &
Then what I see is: [1]+ Stopped lein ring server. Then I'm trying to use:
$ fg 1
And also see that it's stopped. What do I do wrong and how can I run ring as a background process?
As lein simply launches a Java process, this answer of course does not apply to ring/leiningen only.
The simplest way might be to use one of the following Linux/UNIX utilities (and some more as well):
screen
detachtty
nohup

starting and stopping a daemon at user login logout

I have a daemon script written in ruby which responds to commands like daemon start and daemon stop. It's executable with shebang #!/usr/bin/env ruby and it works invoked from terminal. I need to start the daemon on login and stop it on logout.
Background info: KDE, zsh.
I already tried to make two separate shell scripts with daemon start and daemon stop and place them in ~/.kde4/Autostart | ~/.kde4/shutdown. The scripts start.sh and stop.sh are working in terminal, but no luck in autostart or shutdown.
I can't put them in .zshrc respectively .zlogout, because I start many login shells in a work session.
So I am stuck :) Any ideas?
Update: F1 => Help :)
You could try running the program as an autostart app, and then have it watch to see when its parent (probably the session manager) stops running.

Resources