Send command to a nohup process? - linux

I have a minecraft server that when I run it, takes the console and can receive commands/parameters.
I'm running it with nohup java -Xms.... -jar spigot.jar &. It will stay in the background with PID XXXX and port YYYY.
I want know if it is possible to send commands to it as /help.
Regards and thanks for the help.

Instead of running your server in the background with nohup java -Xms.... -jar spigot.jar & you could use a terminal multiplexer like screen.
See https://ss64.com/bash/screen.html
or https://www.gnu.org/software/screen/manual/screen.html
For interactive start, run screen first, then inside the screen session run java -Xms.... -jar spigot.jar (in the foreground, without nohup or &).
Then you can use screen's escape sequence CTRL+a d to detach from the session. Your server will continue to run.
If you later want to interact with the server, use screen -r. This will reattach your terminal to the session.
Type /help or whatever you need to do.
When you are done, detach from the session again.
You could also use screen -d -m java -Xms.... -jar spigot.jar to create a detached session with your command, e.g. in a startup script.
screen has a lot more capabilities. Read the documentation.

I can solved this by tripleee.
This works "nohup tail -f /usr/server/console.in | nohup java -Xms.... -jar spigot.jar >> /usr/server/console.out &"
Whit"echo command >> /usr/server/console.in"
Is as i run the command in the server.

Related

Minecraft Linux Server | Start.sh problem

I have a Minecraft Server running on Linux.
I use to start the server, a start.sh file with following content:
(This content starts a screen session and the minecraft server)
screen -S {ScreenSession} java -Xmx2G -Xms2G -jar spigot-1.18.1.jar
If I use /restart ingame, the screen session will end and the server won't start. So I have to go into the Linux Server and start the Minecraft Server again.
My question:
How can I make it so, if I use /restart that the server will restart with a active screen session.
If have tried many things.
I hope someone can help me,
~Kitty Cat Craft
There is multiple way to achieve what you want.
If you have lot of servers, you can use a quick bash script with an auto restart like that:
#!/bin/sh
while true
do
java -Xmx2G -Xms2G -jar spigot-1.18.1.jar --nogui
sleep 5
done
When you will stop, it will wait 5 seconds then restart.
With this, you can use: screen -dmS <screenName> sh myScript.sh which will run the script into another screen. It's usefull when you run it from a script which run lot of server, like that:
screen -dmS srv1 sh srv1.sh
screen -dmS srv2 sh srv2.sh
screen -dmS srv3 sh srv3.sh
You can also, if you have only one server, just firstly use screen -S screenName. Then, when you are in the screen, run the script that restart automatically (the script that I gave at first).
Also, prefer use /stop than /restart, because spigot will try to find the script. And if it success, it will run a second time the same script, and so will have ghost process.

How to execute spark-shell from file with nohup?

I have a scala script file that gets successfully executed via interactive spark-shell in a classic way: type spark-shell, paste script, wait till completion.
I want to be capable to leave this thing working and exit ssh session, get back to results when I need.
I tried this and it behaves strangely
spark-shell -i file.scala >> out.log 2>&1 &
It prints only several lines of usual spark output to out.log and then reports that the process has ended. When I do 'ps aux | grep spark' I see there is spark running among processes.
When I run this it behaves as expected, but I have to leave session open to have my results.
spark-shell -i file.scala
Is there a way to get spark-shell working with nohup properly?
I know there is spark-submit working with jars but it feels less intuitive, for a simple test I have to asseble a jar and do maven magic.
I encountered the same behavior of spark-shell with nohup. The reasons behind this are unclear, but one can use tmux instead of nohup as a work-around. A pretty good guide on how to use tmux can be found here.
Possible set of actions is as following:
$ tmux new -s session-name
$ ./bin/spark-shell
# do usual stuff manually
Then if you close the terminal window and exit ssh session, you can re-enter the tmux session like this:
$ tmux attach -t session-name
I use a shell script to execute spark-shell, inside my-script.sh:
$SPARK_HOME/bin/spark-shell < $HOME/test.scala > $HOME/test.log 2>&1 &
Read it somewhere by googling and tried it. It is working on my end.
If you are trying to execute it on aws-cli then you use the below command ..
nohup bash -c "YOUR_COMMAND 2>&1 &"
So to execute the spark-shell
nohup bash -c "spark-shell -i file.scala >> out.log 2>&1 &"
Old question, but did you actually try to use the nohup command?
Simply using & to background a process does not prevent it from exiting if it receives a SIGHUP signal, which is what the process will receive when you log out.
Try this:
nohup spark-shell -i file.scala >> out.log &
I'm a bit late to the party, but I recently discovered another solution:
echo ":load myscript.scala" | nohup $SPARK_HOME/bin/spark-shell [other args]
where other args represent more arguments passed to the spark-shell (not to your script; I haven't tested that part). I have a df.write() call at the end of the script so the results are saved to HDFS - no need to have them printed on screen. Note I don't need an & at the end of the command.
I have tried closing the SSH connection and the spark-shell job keeps running tasks, according to the Spark UI :-)

Run a script after killing lxsession (xorg)

I am trying to run a program automatically within a bash script after killing the LXDE session. My script consists of:
#!/bin/sh
pkill lxsession;
sh /home/pi/RetroPie/EmulationStation/emulationstation
I tried this as well:
#!/bin/sh
nohup & pkill lxsession &
writevt /dev/tty1 'emulationstation'
My aim is to log out of the LXDE session and run EmulationStation on my Raspberry Pi with a bash script. I'm using pkill lxsession; to bypass lxsession's logout confirmation dialog.
As it stands, this script just gets me to the command line from a working LXDE desktop. Thanks for reading.
Dont EmulationStation need some sort of X server running in the background for it to work?
IF not, then try the following:
#!/bin/sh
pkill lxsession;
sleep 5
su -c sh /home/pi/RetroPie/EmulationStation/emulationstation
exit
It could also be that when you log out of your lxde session the emulationstation dosent have a usershell to open it, therefore "su -c"
I'm not sure if its going to work but I hope you solve it. :)

How to start a process that won't end when my ssh session ends?

Somehow this isn't yielded by a google search.
I'm scripting a server in node.js. I start the server by executing its script with the node program:
node myserver.js
But the server staying up is dependent on my ssh session. How can I make this (and all such processes) persistent? Init.d?
Use the nohup command:
From http://en.wikipedia.org/wiki/Nohup
nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.
Try this:
nohup node myserver.js &
Have you tried GNU screen? Using it, a process can continue to run when you end your ssh session. nohup is a standard *nix command that will allow you to do the same, albeit in a more limited way.
Use screen. Type screen from the terminal and then launch your process. If you disconnect you can reconnect to the ssh session, by typing 'screen -ls' (to see active screens) and 'screen -r' to reconnect.
The program needs to run in a daemonized mode. Here's a good post for doing this in Ubuntu.
nohup is good to run the job under. If the job is already running, you can try disown -h (at least in bash)

How to run infinitely script in background on Linux?

I have a PHP script with infinite loop. I need this script running forever. So, I run
php /path/to/script.php > /dev/null &
And it works in background in my current user's security context. But when I close terminal window (log off), of course, CentOS Linux kills my program.
I see two guesses: run from a different user in background or make a daemon. I need help in each situation.
Thanks a lot!
nohup is your friend.
nohup command &
I think the general solution to that is nohup:
nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.
nohup is most often used to run commands in the background as daemons. Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected. This command is very helpful when there is a need to run numerous batch jobs which are inter-dependent.
nohup is your friend.
You could:
Install screen and run the command from there. screen is a persistent terminal session that you can leave running.
Write an init/upstart (whatever you use) script so it loads on boot
Use the pear lib system_daemon
Use cron if batch work fits the scenario better (just remember to check for running instances before you launch another, iff concurrency is an issue)
Edit: or as everybody else and their brother has just said, nohup
Using command
nohup your_command &
For example
nohup phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003 &
here "phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003" was my command

Resources