I need to launch a script that will act like a daemon from another script and will not prevent me from being able to close Terminal window.
I tried these standard solutions inside my script :
./myscript.sh arg1 arg2 &
Or this
nohup ./myscript.sh arg1 arg2 &
but they all prevent me from being able to close Terminal window
I don't need any output from that subsidiary script.
Do you know a way to do that in standard UNIX shell, or if I can find some builtin commands or services on Mac to ask to run that script on my behalf.
Thanks
This might work:
xterm -e 'sh /path/to/script.sh' &
You may also want to include the -hold option as well.
Related
I am working on a Raspberry Pi powered Magic Mirror project and to start the program I execute a shell script that runs in the background continuously. To make the AI part of my project work I need to open a second shell script in the background that also runs continuously. My problem occurs when I try to execute my Xterm commands it waits for the first script to complete before it starts the second script. Since both scripts have no designated end point I am stuck. Is there a way to make both Xterm commands execute at the same time?
Here is my current code to start the Xterm sessions:
cd ~/MMStartAll
xterm -e "cd ~/MMStartAll; ./AssistantStart.sh"
xterm -e "cd ~/MMStartAll; ./MMStart.sh"
$SHELL
Your script should end with a &. This means that both the xterms will run in a sepperate process id (pid).
cd ~/MMStartAll
xterm -e "cd ~/MMStartAll; ./AssistantStart.sh" &
xterm -e "cd ~/MMStartAll; ./MMStart.sh" &
$SHELL
I have a shell script and I want the session text to be saved automatically every time the script runs, so I included the command "script -a output.txt" at the beginning of my script. However, the script stops running after this line of code, which only displays a "bash-3.2$" on the screen and won't go on. Any ideas?
Thanks in advance!
The problem is script starts a separate sub-shell than the one that is running the actual script, to club them together. Use the -c flag in script
-c, --command command
Run the command rather than an interactive shell. This makes
it easy for a script to capture the output of a program that
behaves differently when its stdout is not a tty.
Just do,
script -c 'bash yourScript.sh' -a output.txt
I have a VM that I want running indefinitely. The server is always running but I want the script to keep running after I log out. How would I go about doing so? Creating a cron job?
In general the following steps are sufficient to convince most Unix shells that the process you're launching should not depend on the continued existence of the shell:
run the command under nohup
run the command in the background
redirect all file descriptors that normally point to the terminal to other locations
So, if you want to run command-name, you should do it like so:
nohup command-name >/dev/null 2>/dev/null </dev/null &
This tells the process that will execute command-name to send all stdout and stderr to nowhere (instead of to your terminal) and also to read stdin from nowhere (instead of from your terminal). Of course if you actually have locations to write to/read from, you can certainly use those instead -- anything except the terminal is fine:
nohup command-name >outputFile 2>errorFile <inputFile &
See also the answer in Petur's comment, which discusses this issue a fair bit.
I have a bash script with a lot of lines using the command gnome-terminal so it can open several terminals an execute programs. The problem is that some of the programs depend of the executing of one in particular that takes some time, so I need to be sure that this line stop running before the bash can continue with the next.
One way to do it is to put a wait time with "sleep", calculating how much time this program needs to run for completed; but someone know a more efficient way?
Thank you.
Instead of
xterm -e program1 &
xterm -e program2 &
use
program1
program2
or if you absolutely need them to run in an xterm,
xterm -e sh -c 'program1; program2'
The more sane solution is to factor out the xterms from the actual script and do
xterm -e path/to/yourscript &
when you want your script to run in an xterm.
There is an interactive shell console, I can get into it, run specific set of commands inside the console and exit from it.
Now I want to write a bash script that connects to an interactive shell console and runs my commands silently, exits at the end without any interaction. This means I want to have everything automated in a non-interactive way. Any ideas how can I achieve this?
I am trying something like, say, blabla shell is the interactive console here, it always bring me to the interactive mode :(
/usr/bin/blabla shell << EOF
do A,
do B,
do C
quit
EOF
I have a long/specific version of this question can be found here ->
Configure flume in shell/bash script - avoid interactive flume shell console
Closing stdin should do the trick:
exec <&-
The expect command if your friend. It can emulate interactive communication with other commands even in very sophisticated way.
From man expect:
Expect is a program that "talks" to other interactive programs according to a script.
You can try putting the commands you would input in the interactive prompt into a file, then run the command like:
command < file
Maybe the Secure SHell, ssh does what you need. It requires that the "remote" machine is configured as an SSH server. I use it regularly to run commands on other hosts, such as
ssh user#host command