`read` command causes forked process to happen in foreground - bash

Hello I am trying to write a bash script to launch QEMU in the background and wait for the user to press a key to continue with the script.
This is what I have currently:
setup_for_greengrass # these are functions
run_qemu & # fork function and try to run in the background
echo "Press anything to continue once VM is finished booting...\n"
read fullname # wait for user to press a key
install_greengrass
However, what I get in the terminal is the QEMU console and I am unable to keep moving forward with the script. If I fork the process and don't have the read command there, it works as expected and the QEMU console does not show up and the script keeps moving forward.
Any suggestions on how I could fork the QEMU process differently or wait for user input?

I figured it out... In bash version 4 or greater and zsh have support for this command called coproc.
https://www.geeksforgeeks.org/coproc-command-in-linux-with-examples/
https://stackoverflow.com/a/68407430/4397021
https://www.zsh.org/mla/users/2011/msg00095.html
So write the script as follows and it should launch the qemu in the background and let the script keep going forward.
#!/bin/zsh # Make sure to use zsh or upgrade your version of bash
setup_for_greengrass
coproc run_qemu # launch qemu in the background
echo "Press anything to continue once VM is finished booting...\n"
read fullname # wait for user to press a key
install_greengrass

Related

Sending a command in the windows command prompt that terminates after a certain time

I am using a batch script to create a short functional test for technicians at my work which would normally require them to send several commands back-to-back themselves.
There is a command that brings up an image on the test device, but the command hangs until you press ctr+c. When doing everything manual that's fine but I don't want the technicians to do that during the script because they might accidentally exit out of the whole thing.
Is there a way to make it so the script can stop that command and move on the next line in the script? Something like a timeout on the command? Or a key press that just stops the command but doesn't close the script?
Code sample:
echo "Booting Device..."
adb start device
timeout 40
adb shell load_image yosimite.png
timeout 5

Automate a Ruby command without it exiting

This hopefully should be an easy question to answer. I am attempting to have mumble-ruby run automatically I have everything up and running except after running this simple script it runs but ends. In short:
Running this from terminal I get "Press enter to terminate script" and it works.
Running this via a cronjob runs the script but ends it and runs cli.disconnect (I assume).
I want the below script to run automatically via a cronjob at a specified time and not end until the server shuts down.
#!/usr/bin/env ruby
require 'mumble-ruby'
cli = Mumble::Client.new('IP Address', Port, 'MusicBot', 'Password')
cli.connect
sleep(1)
cli.join_channel(5)
stream = cli.stream_raw_audio('/tmp/mumble.fifo')
stream.volume = 2.7
print 'Press enter to terminate script';
gets
cli.disconnect
Assuming you are on a Unix/Linux system, you can run it in a screen session. (This is a Unix command, not a scripting function.)
If you don't know what screen is, it's basically a "detachable" terminal session. You can open a screen session, run this script, and then detach from that screen session. That detached session will stay alive even after you log off, leaving your script running. (You can re-attach to that screen session later if you want to shut it down manually.)
screen is pretty neat, and every developer on Unix/Linux should be aware of it.
How to do this without reading any docs:
open a terminal session on the server that will run the script
run screen - you will now be in a new shell prompt in a new screen session
run your script
type ctrl-a then d (without ctrl; the "d" is for "detach") to detach from the screen (but still leave it running)
Now you're back in your first shell. Your script is still alive in your screen session. You can disconnect and the screen session will keep on trucking.
Do you want to get back into that screen and shut the app down manually? Easy! Run screen -r (for "reattach"). To kill the screen session, just reattach and exit the shell.
You can have multiple screen sessions running concurrently, too. (If there is more than one screen running, you'll need to provide an argument to screen -r.)
Check out some screen docs!
Here's a screen howto. Search "gnu screen howto" for many more.
Lots of ways to skin this cat... :)
My thought was to take your script (call it foo) and remove the last 3 lines. In your /etc/rc.d/rc.local file (NOTE: this applies to Ubuntu and Fedora, not sure what you're running - but it has something similar) you'd add nohup /path_to_foo/foo 2>&1 > /dev/null& to the end of the file so that it runs in the background. You can also run that command right at a terminal if you just want to run it and have it running. You have to make sure that foo is made executable with chmod +x /path_to_foo/foo.
Use an infinite loop. Try:
while running do
sleep(3600)
end
You can use exit to terminate when you need to. This will run the loop once an hour so it doesnt eat up processing time. An infinite loop before your disconnect method will prevent it from being called until the server shuts down.

how to send ssh job to background

I logged in to a remote server via ssh and started a php script. Appereantly, it will take 17 hours to complete, is there a way to break the connection but the keep the script executing? I didn't make any output redirection, so I am seeing all the output.
Can you stop the process right now? If so, launch screen, start the process and detach screen using ctrl-a then ctrl-d. Use screen -r to retrieve the session later.
This should be available in most distros, failing that, a package will definitely be available for you.
ctrl + z
will pause it. Than type
bg
to send it to background. Write down the PID of the process for later usage ;)
EDIT: I forgot, you have to execute
disown -$PID
where $PID is the pid of your process
after that, and the process will not be killed after you close the terminal.
you described it's important to protect script continuation. Unfortunately I don't know, you make any interaction with script and script is made by you.
continuation protects 'screen' command. your connection will break, but screen protect pseudo terminal, you can reconnect to this later, see man.
if you don't need operators interaction with script, you simply can put script to background at the start, and log complete output into log file. Simply use command:
nohup /where/is/your.script.php >output.log 2&>1 &
>output.log will redirect output into log file, 2&>1 will append error stream into output, effectively into log file. last & will put command into background. Notice, nohup command will detach process from terminal group.
At now you can safely exit from ssh shell. Because your script is out of terminal group, then it won't be killed. It will be rejoined from your shell process, into system INIT process. It is unix like system behavior. Complete output you can monitor using command
tail -f output.log #allways breakable by ^C, it is only watching
Using this method you do not need use ^Z , bg etc shell tricks for putting command to the background.
Notice, using redirection to nohup command is preferred. Otherwise nohup will auto redirect all outputs for you to nohup.out file in the current directory.
You can use screen.

How do I launch a program inside a shell script and have the shell script continue, even though the program remains open

I am using bash on Ubuntu. I would like to have a shell script open a program and continue on to the next line of the shell script, even though the program has not terminated.
Adding an & to a command places it in background.
example:
/path/to/foo
/path/to/bar # not executed untill foo is done
/path/to/foo & # in background
/path/to/bar & # executes as soon as foo is started
Read more about job-control here and here
Use something like this (my-long-running-process &) . This will launch your script as a separate process in the background.
You must run the process in the background, but you must enable job-control first. Otherwise, you cannot kill or bring the process to foreground if desired.
To enable job-control, execute:
set -m
To run some task in the background, execute:
task &
To manipulate the background task, use the jobspec syntax (%[n]). For example, to kill the last launched process, execute:
kill %
Note that enabling job-control is required only if you're actually running a script (as stated in the question). If running interactively, job-control is already enabled by default.
The manpage for bash has much more information in the JOB CONTROL section.
http://ubuntuforums.org/showthread.php?t=1657602
It looks like all you have to do is add a & at the end of the line.

Run Ruby script in the background

I have a Ruby script that I need to have running all the time in my Linux box. I tried nohup ruby ruby.rb& but it seems it doesn't work.
How can I have the script running in background?
Have a look at screen which is a command-line utility. Start it with
screen
You will get a new shell which is detached. Start your script there with
ruby whatever.rb
And watch it run. Then hit Ctrl-A Ctrl-D and you should be back at your original shell. You can leave the ssh session now, and the script will continue running. At a later time, login to your box and type
screen -r
and you should be back to the detached shell.
If you use screen more than once, you will have to select the screen session by pid which is not so comfortable. To simplify, you can do
screen -S worker
to start the session and
screen -r worker
to resume it.
Depending on your needs:
fork do
Process.setsid
sleep 5
puts "In daemon"
end
puts "In control script"
In real life you will have to reopen STDOUT/STDERR.

Resources