xterm doesn't run shell script - linux

so I'm trying to use this command:
export DISPLAY=:1; /usr/bin/xterm -hold -e /path/to/shscript
Where shscript is:
#!/bin/bash
echo "Restarting ... $(date)" >> /var/log/mw2.txt
if screen -ls | grep -q 'test'; then
screen -X -S test quit
sleep 1000
screen -d -m -S test wine iw4m.exe -dedicated -console +dw_licensefile license.dat +set net_port "28960" +set party_maxplayers 18 +exec server.cfg +map_rotate +set fs_game "mods/tsd"
else
screen -d -m -S test wine iw4m.exe -dedicated -console +dw_licensefile license.dat +set net_port "28960" +set party_maxplayers 18 +exec server.cfg +map_rotate +set fs_game "mods/tsd"
fi
So I want to launch a new xterm window and it to run a shell script.
But whatever shell script I try to use, it doesn't run it. It just goes blank. All other commands work, but when I put a shell script in it, it just goes blank and does nothing. I can't find a solution for this, please help, thank you.
I get this with set -x:
++ date
+ echo 'Restarting ... (date)'
+ grep -q test
+ screen -ls
+ screen -d -m -S test -wine iw4m etc...

I think you get exactly what you have asked for...
From screen's man page:
-d -m Start screen in "detached" mode. This creates a new session but
doesn't attach to it. This is useful for system startup
scripts.
On the other hand you use -hold for xterm:
-hold Turn on the hold resource, i.e., xterm will not immediately
destroy its window when the shell command completes. It will
wait until you use the window manager to destroy/kill the win‐
dow, or if you use the menu entries that send a signal, e.g.,
HUP or KILL.
And that's exactly what you see. xterm starts, executes screen which runs, but you don't see the output as screen does not attach to the virtual terminal. Since screen has detached the shell script exits and xterm holds the window for you to destroy it at your leisure.
I bet that if in another xterm you attach to the session with screen -S test you'll see the output.

Yep. It was the wine command failing. I changed some stuff, and it works now. I had to put cd /home/MW2 in the script for it to be able to run the iw4m.exe, then screen -S etc.

Related

Startup script to run detachted screen, switch user, and run multiple bash commands

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.

how to create and move between screens in a bash.sh script [duplicate]

Was wondering how I can start up a command such as:
while :; do ./myCommand; done;
But instead of doing the usual
screen -S nameOfMyScreen
Then the command
while :; do ./myCommand; done;
Then detach the screen
^a ^d (Control "a" the control "d"
I would like it to start and detach. Thanks!
screen -d -m sh -c "while :; do ./myCommand; done;"
Explanation:
-d -m starts screen in detached mode (create session but don't attach to it)
sh -c commandline starts a shell which executes the given command line (necessary, since you are using the while builtin).
From screen -h, these look useful:
-dmS name Start as daemon: Screen session in detached mode.
-X Execute <cmd> as a screen command in the specified session.
I haven't done this myself, but that's where I'd start.
Update:
The top of the help also says
Use: path/to/screen [-opts] [cmd [args]]
so the -X switch may be to execute a screen command as opposed to a shell command. You might just be able to put your command after the -dmS <name> without any -X switch.

How to run a script in background (linux openwrt)?

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.

Shell script statements not running one after one

I wrote a shell script that calls some other shell scripts in a new terminal window. It was working fine on my computer. its like this
#!/bin/sh
gnome-terminal -e "sh one.sh"
zenity --info --text "exed one"
gnome-terminal -e "sh 2.sh"
zenity --info --text "exed 2"
firefox "www.aurl1.com" "www.aurl2.com"
According what I understood, the script will first open a terminal and run the first script, after finishing that it will show the zenity then run two in new terminal then show zenity like that. But when the same was exed on another computer, the script is not following this order. It simply opens all terminal side by side not waiting one to finish and showing the dialogues together. Why is this problem ? Thanks in advance.
It used to be that gnome-terminal would run each terminal in its own process. But now gnome-terminal checks on startup if there is already a gnome-terminal process running, and if so, tells the existing terminal process to open a new window with the given arguments. There is no problem with your script: it is waiting for each process to finish in turn, it is just that gnome-terminal is exiting immediately after passing off its work to a different process.
According to man gnome-terminal, you can use the --disable-factory option to disable this behaviour, and run each command in its own process.
Try this:
gnome-terminal --disable-factory -e 'sh -c "echo hi && sleep 10"'
echo now you can run script 2
Run them like this:
gnome-terminal -e "sh one.sh" ; zenity --info --text "exed one" ; gnome-terminal -e "sh 2.sh" ; zenity --info --text "exed 2"
What the comma does is it tells the shell to execute the first command, wait for it to finish and only then move on to the next. Otherwise, the behavior you described is normal, it parses the lines one by one, without waiting for anything.

How can I send stuff commands to a start-in-detached screen?

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

Resources