I'm running an Ubuntu 16 server. Mainly I have an application running. I use Plesk, WinSCP and PuTTY to manage the server, the files, and to run this app. The app it's a .jar which I allocate RAM and run.
This app has a console which I run into an screen on PuTTY. If the app crashes, I need to go into that screen and run again the line that allocates RAM and launch the app again.
So here's my question:
Could you help me to see if the script I wrote is wrong or can be better/optimized?
The intention is that if the app crashes, it's automatically launched after some seconds. If the screen is not found because was shut down, the screen has to be made again and so the app launched again. If also it crashes too many times, then I don't know if it would be nice to put some kind of code to prevent restarting all the time something that would crash every time, just in case it starts a loop of crashes.
This app of course it's on a directory of the ftp and I guess that some code parts, of what I exposed, would need the directory path/rute (C:/ftpRoot/mainFolder/anotherFolder/appFolder).
If I need to give you any extra information just tell me and gladly I will.
Thank you all in advance.
Here's the .sh I have for the moment:
for session in $(screen -ls | grep -o '[0-9]\{3,\}\.\S*')
do
screen -r DedicatedScreen -p0 -X stuff "&9Server is restarting. \015"
screen -r DedicatedScreen -p0 -X stuff "stop\015" #Send "stop\r" to the RunningApp console.
done
counter=0
while [ $(screen -ls | grep -c 'No Sockets foun in') -lt 1 ]; do
if [ $(( $counter % 10)) -eq 0 ]; then
echo 'A previous server is in use. Waiting for 10 seconds before starting server ...'
fi
sleep 1
counter=$((counter+1))
done
echo 'Starting Application...'
screen -dmS "DedicatedScreen" java -Xms1024M -Xmx7168M -jar custom_f.jar
sleep 1
while [ $(screen -ls | grep -c 'No Sokets found in') -ge 1 ]; do
sleep 5
screen -dmS "DedicatedScreen" java -Xms1024M -Xmx7168M -jar custom_f.jar
done
echo 'Application started.'
Related
I had this idea of a script that I was trying to make come to light. I want one main process that is running and keeping track of subprocesses running in the background. I decided to go with screen for my implementation, and I can run the main.sh in attached mode, with the subcommands all running in detached. I can also send text to the main.sh once each sub-process has hit their point. However, i want the main script to be running until all sub-processes finish (accomplished), but I want it to be updating what it is printing out to the user. I currently have the main process doing:
while screen -list | grep -q process-1 || screen -list | grep -q process-2 || screen -list | grep -q process-3; do
sleep 1
done
which works perfectly as the main loop, but any data i send to that process just prints out like text, not like a command. Is there a way I can keep a screen session alive and receiving more commands/variables?
I currently plan on sending data from the sub-process like screen -S main -X stuff "PROCESS_1=FINISHED" and the main will keep trying to grab the variable PROCESS_1 to get it's status.
I can't just get the result of the sub-process either, as for at least one of the commands i plan on it running continuously, but I want to let the main-process know when it has hit a certain point. I was also tinkering with the idea of using file descriptors but I had issues getting that working in detached mode.
Is this possible and I haven't just used/found the right command? Do i need to somehow use another screen as a like a data layer so that the main layer just prints out to the user?
For completeness, here is the current setup:
start.sh
#!/bin/bash
screen -S process-1 -dm ./process-1.sh
screen -S process-2 -dm ./process-2.sh
screen -S main -m ./main.sh
main.sh
#!/bin/bash
while screen -list | grep -q process-1 || screen -list | grep -q process-2; do
sleep 1
done
process-1.sh
#!/bin/bash
count=0
while [[ count -ne 5 ]]; do
((count+=1))
sleep 5
done
screen -S main -X stuff "PROCESS_1=FINISHED"
process-2.sh
#!/bin/bash
count=0
while [[ count -ne 3 ]]; do
((count+=1))
sleep 5
done
screen -S main -X stuff "PROCESS_2=FINISHED"
Thanks for any advice you can provide.
EDIT:
Something along these lines is the end goal, like Vue.js, a constant screen that updates whenever changes are made (or in this case processes end)
I want to write a bash script where I check If my Screen (I gave this screen the name a3_altis) is already running or not, just like this:
if (screen a3_altis is running)
then
./stop.sh
sleep 5
./start.sh
else
./start.sh
fi
I'm new in Bash, so I don't really know how to check If a screen is running.
screen may provide a more robust mechanism, but it should suffice to just use grep:
if screen -ls | grep -q a3_altis; then
./stop.sh
sleep 5
fi
./start.sh
I read many threads about how to do this and yet it doesn't work for me.
What I want to achieve is:
When my laptop has 2 additional displays connected when I close its lid, I don't want it to go to sleep. However, if I disconnect the displays and the lid is closed, I want to put the laptop to sleep (this way, not forgetting it on).
Therefore, I created a BASH script that should be executed on VGA / HDMI connection events. The BASH script counts how many displays are connected and if there's only 1, it will put the laptop to sleep when the lid is closed.
I have Ubuntu 14.04 LTS. This is what I've done so far:
Created 2 files: displays_count_sleep.sh and on_hdmi_connected.rules
https://gist.github.com/nbtk123/9ffbf7541e47b9c0015f5c3e9f44b7c9
Put this on_hdmi_connected.rules into /etc/udev/rules.d to catch the event:
SUBSYSTEM=="drm", RUN+="/bin/bash /home/nir/dev/scripts/displays_count_sleep.sh"
Put the bash script I want to run, displays_count_sleep.sh, into /home/nir/dev/scripts:
#!/bin/bash
DISPLAYS_NUM=2
`touch test`
display_count=`xrandr -d :0 -q | grep ' connected' | wc -l`
echo "display count="$display_count
echo "display_num="$DISPLAYS_NUM
if [ "$display_count" -ge "$DISPLAYS_NUM" ]; then
echo "nothing"
`gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action nothing`
`gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action nothing`
else
echo "sleep"
`gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action suspend`
`gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action suspend`
fi
Both files have been created as root. When running "udevadm test", the output is as in the file 'udevadm_test_output' on the link.
I don't know if the event is catched, but on the real monitor (dis)connection, the script doesn't run. It works okay if I run it manually.
I have a raspberry pi and I want to start a java application for 5 hours, kill it and start it again.
Because I need to login via SSH I thought it was clever to run the java application within screen (because I want to do other things while the program is running).
So without the script I started with:
screen -S java
java -jar program.jar
And then I send the Keys [CTRL] + [A] + [D] to detach.
Now I need to write this in shell.
I started with:
#!/bin/bash
#Check if app runs right now
OUTPUT="$(screen -ls)"
if [[ $OUTPUT == *"javaapp"* ]]
then
#Say that the javaapp is currently running
echo "javaapp is up!";
#Kill the javaapp!
screen -d javaapp #Does not work
fi
#Start it again
But it does not work :/
And I don't even know how to fix it since it's the first script I am trying to write. Is there anyone who can help me?
(Oh: And I would run the shellscript every 5hrs via. cronjob)
Thanks in advance!
Try this trick via cron:
#!/bin/bash
if [[ -f /opt/javaapp.pid ]]
kill -9 `cat /opt/javaapp.pid`
rm /opt/javaapp.pid
fi
nohup java -jar program.jar > /opt/javaapp.log 2>&1&
echo $! > /opt/javaapp.pid
Edit: I'm not sure it works with java apps though, or with apps that tend to fork and spawn other processes. Watch out for zombies if you try this.
Also, if anybody knows a better way, please share it. Now I'm curious.
Alright, so I have a .sh file that I run that will launch my server with the certain specifics that I'm looking for. It launches the server through screen into it's own screen. Here's the code for my run.sh file.
#!/bin/bash
# run.sh
# conversion of run.bat to shell script.
echo "Protecting srcds from random crashes"
echo "Now launching Garrys Mod RequiemRP"
sleep 5
screen -A -m -d -S gmserver ./srcds_run -console -game garrysmod +maxplayers 32 +map rp_downtown_v6 -autoupdate
echo "Server initialized. Type screen -x to resume"
Usually I use a batch file to do this, but I'm now using linux for my server hosting. Part of that batch file was if srcds (the server itself) were to crash, the run.bat file would restart the server automatically. I'm looking to do this with my run.sh file, but I'm unsure how to.
Perhaps you could make a service or script that will periodically check if the process is running. This will check if it's on and if it isn't, it will turn it on when executed.
#!/bin/bash
ps cax | grep srcds > /dev/null
if [ $? -eq 0 ]; then
exit
else
bash /path/to/run.sh
fi
I tested the command and it works. For my virtualized debian 9 system.