Mac OS: Script that does something, then starts an Application, then waits until it terminates, and finally does something? - bash

On Mac OS is it possible to create an Automator/Bash/Java/ApplieScript that runs an bash-command to do something (for example chance the screen resolution) after that runs an application (for example a game that needs a specific screen resolution) then waits until the application has been terminated and after that does one final thing (for example change the screen resolution again)?
I tried to work with all Automator, Bash, Java and ApplieScript. I even tried to combine multiple of them to one chain of things that runs other things just to run something else until it terminates and then run something else, but non of that semms to work properly.
I got the terminal commands that changes screen resolution and I also got the terminal command that runs the Game, but I can't bring it together in an logical correct chain of things to happen...
The Commands are:
do shell script "/Volumes/Sierra/Users/xyz/Documents/cscreen -x 1600 -y 900 -r 60"
do shell script "open steam://run/8930"
do shell script "/Volumes/Sierra/Users/xyz/Documents/cscreen -x 1280 -y 720 -r 60"

What you want is the -W argument for open:
-W Causes open to wait until the applications it opens (or that were already open) have exited. Use with the -n
flag to allow open to function as an appropriate app for the $EDITOR environment variable.
So in your example I would make a script like this:
#!/bin/bash
/Volumes/Sierra/Users/xyz/Documents/cscreen -x 1600 -y 900 -r 60
open -W steam://run/8930
/Volumes/Sierra/Users/xyz/Documents/cscreen -x 1280 -y 720 -r 60
Now open should not return control to the shell until steam exits.

Related

Send and read serial commands from terminal

I have a very limited list of software that I can install on an (IOT edge) device. I have minicom and chat commands (in addition to standard commands like echo and cat), and need to write to a serial device a command and read the response.
The device in question is a modem, and I need to run AT commands on it. If using minicom and setting up the menu etc. I can run these commands normally, and get the output. The problem is that I have around a thousand of these devices, so setup and data logging needs to be automated.
So within these parameters is there a way to run minicom and capture the output without any interactive elements? I have tried
minicom -S scriptfile -C outfile
where scriptfile (for now) contains following:
sleep 1
send "AT"
This seems to ignore the sleep command, and outfile is created, but is left empty. Also what would I need to add to the command that it wouldn't open a session or interactive element?

Gnu Screen hardcopy without joining screen

I writing a python app that runs some commands on a gnu screen without joining the screen and seeing the hardcopy buffer. In particular, I am running:
screen -dmS test
screen -S test -p 0 -X stuff "ls$(printf \\r)"
screen -S test -X hardcopy screenOutput.txt
when I look at screenOutput.txt, I see nothing.
However, if I join the screen, and then run hardcopy,
screen -dmS test
screen -S test -p 0 -X stuff "ls$(printf \\r)"
screen -r test
(quit the screen with c-A c-D)
screen -S test -X hardcopy screenOutput.txt
Then I see the output in screenOutput.txt.
Is there a way to get hardcopy to write to the file, without joining the screen?
See linux - Send command to detached screen and get the output - Unix & Linux Stack Exchange for an example on how to send commands to a session. One correct invocation is:
screen -dmS test
screen -S test -X hardcopy screenOutput.txt
I.e. -X shall be the first command option. -d/-r switches to search only attached/detached sessions have to follow it (the docs are notoriously vague on this); in this case, they are not needed at all.
I confirmed this to not work (produce blank file) in screen v4.0.3 and work in v4.2.0 and up in the same environment.
Extensive investigation:
Debugging shows that the root cause is in WriteFile at fileio.c:472 : if (!fore) break; which quits the function without writing anything because fore (a pointer to the foreground window) is indeed NULL.
I couldn't pinpoint the specific commit where this was fixed, but did check that it isn't NULL in v4.2.0. The variable is set in a number of places around the codebase and is reset to NULL in roughly the same amount of places, often in code that follows the assignment. So the chances of a working workaround are very slim.
I hereby reaffirm that in screen v4.0.3, hardcopy is broken and you have to upgrade (e.g. install a version to /usr/local so that it overrides the stock one).

Pass command from scheduled script to program running in xterm window

I have a game server running in an xterm window.
Once daily I'd like to send a warning message to any players followed after a delay by the stop command to the program inside the xterm window from a script running on a schedule. This causes the cleanup and save functions to run automatically.
Once the program shuts down I can bring it back up easily but I don't know how to send the warning and stop commands first.
Typed directly into xterm the commands are:
broadcast Reboot in 2 minutes
(followed by a 2 minute wait and then simply):
stop
no / or other characters required.
Any help?
Do you also need to type something from the xterm itself (from time to time) or do you want your server to be fully driven from external commands?
Is your program line-oriented? You may first try something like:
mkfifo /tmp/f
tail -f /tmp/f | myprogram
and then try to send commands to your program (from another xterm) with
echo "mycommand" > /tmp/f
You may also consider using netcat for turning your program to a server:
Turn simple C program into server using netcat
http://lifehacker.com/202271/roll-your-own-servers-with-netcat
http://nc110.sourceforge.net/
Then you could write a shell script for sending the required commands to your "server".
If you know a little about C programming; I remember having once hacked the script program (which was very easy to do: code is short) in order to launch another program but to read commands from a FIFO file (then again a shell script would be easy to write for driving your program).
Something you might try is running your program inside a screen session.
You can then send commands to the session from cron that will be just
as if you typed them.
In your xterm, before launching the program do:
screen -S myscreen bash
(or you can even replace bash by your program). Then from your cron
screen -S myscreen -X stuff 'broadcast Reboot in 2 minutes\n'
sleep 120
screen -S myscreen -X stuff 'stop\n'
will enter that text. You can exit the session using screen -S myscreen -X quit
or by typing ctrl-a \.
screen is intended to be transparent. To easily see you are inside screen, you can
configure a permanent status bar at the bottom of your xterm:
echo 'hardstatus alwayslastline' >~/.screenrc
Now when you run screen you should see a reverse video bottom line. Depending
on your OS it may be empty.

Buffered pipe in bash

I'm running a Bukkit (Minecraft) server on a Linux machine and I want to have the server gracefully shut down using the server's stop command and the computer suspend at a certain time using pm-suspend from the command line. Here's what I've got:
me#comp~/dir$ perl -e 'sleep [time]; print "stop\\n";' | ./server && sudo pm-suspend
(I've edited by /etc/sudoers so I don't have to enter my password when I suspend.)
The thing is, while the perl -e is sleeping, the server is expecting a constant stream of bytes, (That's my guess. I could be misunderstanding something.) so it prints out all of the nothings it receives, taking up precious resources:
me#comp~/dir$ ...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
Is there any such thing as a buffered pipe? If not, are there any ways to send delayed input to a script?
You may want to have a look at Bukkit's wiki, which recommends an init script for permanently running servers.
This init script uses rather unconventional approach to communicate with running server. The server is started in screen session, then all commands are send to the server console via screen, e.g.
screen -p 0 -S $SCREEN -X eval 'stuff \"stop\"\015'
See https://github.com/Ahtenus/minecraft-init/blob/master/minecraft
This approach suggest that bukkit may be expecting standard input to be attached to a terminal, thus requiring screen wrapper (which is itself terminal emulator) for unattended runs.

how do i start commands in new terminals in BASH script

my bash script reads in a few variables and then runs airodump on my home network. I would like to keep the window with airodump running and open some new terminals to run other commands for network discovery while the airodump screen is open (so i can see the results).
right now what i have looks like this (edited for brevity):
#!/bin/bash
read -p "Enter the channel: $channel"
airomon-ng start wlan0 $channel,$channel
airodump-ng -c $channel,$channel mon0 &&
terminator -e netstat -ax &&
terminator -e nmap 192.168.1.1
the first command that uses the whole terminal (airodump) starts up fine and i can see the network, but then it just stays on that screen. If i ctrl+c then it goes back to prompt but i can see the error : Usage: terminator [options] error no such option
i want the netstat and nmap commands to appear and stay in their own terminal windows, how can i do this?
The terminal window is generated by a separate program from the command running inside. Try one of these variations:
xterm -e airomon-start wlan0 "$channel","$channel" &
gnome-terminal -x airomon-start wlan0 "$channel","$channel" &
konsole -e airomon-start wlan0 "$channel","$channel" &
Pick the command that invokes the terminal program you like. You'll have to do this for every command that you want run in its own window. Also, you need to use a single & at the end of each such command line -- not a double && -- those do totally different things. And the last line of your script should be just
wait
that makes it not exit out from under all the terminals, possibly causing them all to die.
Obligatory tangential shell-scripting nitpick: ALWAYS put shell variable uses inside double quotes, unless you know for a fact that you need word-splitting to happen on a particular use.
If the script is running in the foreground in the terminal, then it will pause while an interactive command is using the terminal. You could change the script to run airodump in a new terminal as well, or you could run the background commands before you start airodump (maybe after sleeping, if you're concerned they won't work right if run first?)

Resources