I currently have a script set to run every 6 hours (0000, 0600, 1200, 1800) that restarts a minecraft server running in a detached GNU screen.
I'd like to create a script that runs on the hour, that will show the remaining time to the next restart.
I.E.: After a restart, every hour the script will send the time remaining to the screen session, which will interpret and print it to the server.
Currently the hourly script looks like this, but I have a feeling i'll run into issues after 6PM:
HOUR=`echo $(($(date +%H) - 6))`
tleft=`echo $((12 % $HOUR))`
screen -X setenv remain "$tleft"
screen -X readbuf $remain
screen -x minecraft -X eval 'stuff "say The Server will restart in $remain hours"\015'
screen -x minecraft -X eval 'stuff "save-all"\015'
Any help is appreciated, thanks
Not sure why I didn't think of this before, but a simple if statement solved my problem:
hour=`echo $(date +%I)`
if [[ $hour -lt 6 ]];
then
tleft=$((6 - $hour))
else
tleft=$((12 - $hour))
fi
screen -X setenv saystring "Server will restart in $tleft hour(s)"
screen -X readbuf $saystring
screen -x minecraft -X eval 'stuff "say $saystring"\015'
screen -x minecraft -X eval 'stuff "save-all"\015'
Related
I want to create a small startup script that does multiple things in a row in a screen.
The script starts a named, detached screen (screen -S discordbot -d -m works)
The user inside the script is changed (Neither screen -S discordbot -X "su discordbot", screen -S discordbot -X su discordbot, nor screen -S discordbot -d -m bash -c "su discordbot;" seems to work, or at least subsqeuent commands are not executed).
A cd folder change is exectuded.
A java jar or other script is started.
As I run multiple bots, the script needs to be able to do this in slight variation multiple times in a row. Any pointers on how this could be done?
The screen session that you start up will exit as soon as the process that you started exits.
This works, for instance:
$ screen -S discordbot -d -m bash
$ screen -ls
There is a screen on:
2948.discordbot (Detached)
1 Socket in <...>
As does this:
$ screen -S discordbot -d -m bin/discordbot.sh
Where bin/discordbot.sh looks like this:
#!/bin/sh
echo "Sleeping..."
sleep 10
/bin/echo -n "Hit enter to finish this script: "
read
The last two lines to prevent the screen from exiting prematurely. The other various things you want to do within that startup script should also work, assuming that you do this as root so that the su will work without prompting.
I have this script:
#!/bin/sh
while [ true ] ; do
urlfile=$( ls /root/wget/wget-download-link.txt | head -n 1 )
dir=$( cat /root/wget/wget-dir.txt )
if [ "$urlfile" = "" ] ; then
sleep 30
continue
fi
url=$( head -n 1 $urlfile )
if [ "$url" = "" ] ; then
mv $urlfile $urlfile.invalid
continue
fi
mv $urlfile $urlfile.busy
wget -b $url -P $dir -o /www/wget.log -c -t 100 -nc
mv $urlfile.busy $urlfile.done
done
The script basically checks for any new URLs at wget-download-link.txt for every 30 seconds and if there's a new URL it'll download it with wget, the problem is that when I try to run this script on Putty like this
/root/wget/wget_download.sh --daemon
it's still running in the foreground, I still can see the terminal output. How do I make it run in the background ?
In OpenWRT there is neither nohup nor screen available by default, so a solution with only builtin commands would be to start a subshell with brackets and put that one in the background with &:
(/root/wget/wget_download.sh >/dev/null 2>&1 )&
you can test this structure easily on your desktop for example with
(notify-send one && sleep 15 && notify-send two)&
... and then close your console before those 15 seconds are over, you will see the commands in the brackets continue execution after closing the console.
The following command will also work:
((/root/wget/wget_download.sh)&)&
This way you don't have to install the 'nohub' command in the tight memory space of the router used for OpenWrt.
I found this somewhere several years ago. It works.
The &at the end of script should be enough, if you see output from the script it means, that stdout and/or stderr is not closed, or not redirect to /dev/null
You can use this answer:
How to redirect all output to /dev/null
I am using openwrt merlin and the only way to get it working was using the crud cron manager[1]. Nohub and screen are not available as solutions.
cru a pinggw "0 * * * * /bin/ping -c 10 -q 192.168.2.254"
works like charm
[1][https://www.cyberciti.biz/faq/how-to-add-cron-job-on-asuswrt-merlin-wifi-router/]
https://openwrt.org/packages/pkgdata/coreutils-nohup
opkg update
opkg install coreutils-nohup
nohup yourscript.sh &
You can use nohup.
nohup yourscript.sh
or
nohup yourscript.sh &
Your script will keep running even if you close your putty session, and all the output will be written to a text file in same directory.
nohup is often used in combination with the nice command to run processes on a lower priority.
nohup nice yourscript.sh &
See: http://en.wikipedia.org/wiki/Nohup
For busybox in Openwrt Merlin system, I got a better solution which combined cru and date command
cru a YOUR_UNIQUE_CRON_NAME "`date -D '%s' +'%M %H %d %m *' -d $(( \`date +%s\`+2*60 ))` YOUR_CMD_HERE"
which add a cron job running 2 minutes later, and only run once.
Inspired by PlagTag's idea.
In another way these code would tried:
ssh admin#192.168.1.1 "/jffs/your_script.sh &"
Simple and without any programs like nohup screen...
(BTW: worked on Asus-Merlin firmware)
Try this:
nohup /root/wget/wget_download.sh >/dev/null 2>&1 &
It will go to the background so when you close your Putty session, it will be still running, and it won't send messages to the terminal.
OPTIONS="java -Xms1024M -Xmx1024M -jar craftbukkit.jar"
PROCESS=server01
screen -dmS $PROCESS $OPTIONS nogui # Starting the application
screen -x $PROCESS -X stuff `printf "stop\r"` # Closing the application
screen -x $PROCESS # Attaching to the terminal of the application
The application works fine at the start, however I get problems with stuff 'printf "stop/r"'
It seems not to work when I just start up, wait some time and then try to stop it with the command above. But the strange thing is, that if I did screen -x $PROCESS and detach (ctrl-A & ctrl-D) and then I use the Stop command it does work. So is there a way around to stuff printf without screen -x $PROCESS?
Adding the argument -p 0 should fix it. Something like this:
screen -x $PROCESS -p 0 -X stuff `printf "stop\r"`
(From screen documentation: -p window Preselect the named window if it exists.)
#!/bin/sh
SERVER=$1
RCON=$2
echo "$SERVER"
echo "$RCON"
sudo -u flash screen -r $SERVER -X stuff "$RCON"`echo -ne '\015'`
Guys do you see something wrong with this code? The main concept in the script is to push a command to a opened screen session.
In particular, it's expected to execute a command into opened screen session.
The script echoes the two vars, but when I open the screen, the command $RCON isn't executed.
replaced
#!/bin/sh
with
#!/bin/bash
Any session that I start with "-d -m" doesn't accept "-X stuff [...]" commands unless I've attached to the screen at least once. There is no error message, the commands just do not get through.
The problem is that I start the session from a cron job and am unable to attach to the screen from within cron.
Steps to repeat
$ screen -m -d -S mydaemon bash
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
32456.mydaemon (Detached)
$ screen -r -S mydaemon
$ ^a d
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
No Sockets found in /var/run/screen/S-user
^a d indicates pressing Ctrl+a then pressing d.
Versions
CentOS release 5.5 (Final)
Screen version 4.00.03 (FAU) 23-Oct-06
Edit: The best answer to this question is this other SO answer. I leave my kludgey solution here, anyway, in case it inspires a solution to a similar problem.
A possible workaround is to use a second, already running and detached screen session to start the screen session to which you want to send the "stuff" command in attached mode, and then send that screen session a detach command and then the stuff command.
$ screen -dmS spawner
$ screen -S spawner -X screen screen -dR mydaemon
$ sleep 1 # may be necessary
$ screen -S mydaemon -X detach
$ screen -S mydaemon -X stuff "whatever"
(Note: the doubled "screen" is not a typo!) You are still left with an unstuffable screen session (spawner) but if the resources it takes are important you can always just use "kill -TERM ..." or its ilk to terminate it (or have it automatically exit after a certain amount of time by starting it with something like
$ screen -dmS spawner bash -c "sleep 60"
or similar).
Have you considered using tmux instead? It is much more suitable for non-interactive tasks.
Thanks for Ron Kaminsky.
I found another way to kill 'spawner' screen
$ screen -dmS spawner
$ screen -S spawner -X screen screen -dR mydaemon
$ sleep 1 # may be necessary
$ screen -S mydaemon -X detach
and add below line
$ screen -S mydaemon -X screen screen -S spawner -dR
$ sleep 1 # may be necessary
$ screen -S spawner -X detach
$ screen -S spawner -X kill
screen -d -m /bin/bash "test.sh"
screen (detached) (mobile) (shell) "What to execute"
$ screen -d -m /bin/bash "test.sh"
$ screen -li
There is a screen on:
8540..Satelite (10/17/2013 04:53:22 AM) (Detached)
1 Socket in /var/run/screen/S-matt.
Using the .screenrc file, you can set things to start;
screen -t <title> <window position> <command>
So, if you wanted to start top in window 7, you'd do
screen -t window7top 7 top