I have a shell script contain loop like this:
while true
do
if ... ; then
...
else
...
break
fi
done
I want this script to run backend while OS start. I have try to add this script into /etc/rc.d/local.rc as startup script. But OS start too long, and after OS start up, this script did not exist.
So how to add this script into backend while OS start up? And I need this script also could be start up by hand in backend. Thank you~
If you are scheduling on either Linux or Mac, then you can schedule through crontab.
Open terminal.
$crontab -e #use sudo to run as administrator
#add below line
#reboot sh /absolute_path/script.sh
Give the absolute path of the script, save the crontab and exit.
The script will start running from the next reboot.
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 wrote a shell script that runs a service. I open the terminal, I run the script that runs the service and, after the script ends, I close the terminal but the service keeps running, and this is what I want.
Anyway, if I run the script through the Gnome command "Run in terminal", when the terminal closes, also the service is killed.
That's very strange, I can't understand why and I'm not able to solve this problem.
Any ideas?
Try executing
nohup ./shell_script &
nohup command makes the process continue executing even after the terminal has closed, ignoring the SIGHUP signal.
Note the script will execute in the background, and the output will be appended to a file.
When I'm done with Terminal, I want to exit it. Right now, I have three options:
killall Terminal. It will end the process, but rather abruptly. I don't think this is the best idea.
Call exit. I've changed the settings so exit closes Terminal. The app still appears open in the Dock though, and doesn't do what I want it to do.
Right click>Quit. However, this isn't a Terminal command to exit it.
So now, what is the way I should exit and close Terminal? I've heard something about osascript but I'm not too sure. I want to exit and close, so that Terminal is no longer open, both as a window and as a process.
in Terminal.app
Preferences > Profiles > (Select a Profile) > Shell.
on 'When the shell exits' chosen 'Close the window'
How about the good old Command-Q?
Actually, you should set a config on your Terminal, when your Terminal is up press ⌘+, then you will see below screen:
Then press shell tab and you will see below screen:
Now select Close if the shell exited cleanly for When the shell exits.
By the above config each time with exit command the Terminal will close but won't quit.
In the Terminal app, Preference >> Profiles tab.
Select the Shell tab on the right.
You can choose Never Ask before closing to suppress the warning.
You could use AppleScript through the osascript command:
osascript -e 'tell application "Terminal" to quit'
In a terminal window, you can type:
kill -9 $(ps -p $PPID -o ppid=)
This will kill the Terminal application process, which is the parent of the parent of the current process, as seen by the kill command.
To close a Terminal window from within a running script, you need to go up one more level in the process hierarchy like this:
kill -9 $(ps -p $(ps -p $PPID -o ppid=) -o ppid=)
I 've been using ctrl + d. It throws you out into the destination where You've started the sqlite3 command in the first place.
osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"
This simulates a CMD + w keypress.
If you want Terminal to quit completely you can use:
osascript -e "tell application \"System Events\" to keystroke \"q\" using command down"
This doesn't give any errors and makes the Terminal stop cleanly.
You can also use this convoluted command, which does not trigger a warning about terminating its own process:
osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit
This command is quite long, so you could define an alias (such as quit) in your bash profile:
alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'
This would allow you to simply type quit into terminal without having to fiddle with any other settings.
Use the osascript command in your code as icktoofay mentioned: osascript -e 'tell application "Terminal" to quit'
Then, open Terminal preferences, go to Settings > Shell, and set "Prompt before closing:" to "Never." Terminal should now quit completely (not remain open in your dock) and ignore the prompt before quitting. If you have only one Terminal window open and the osascript command is your last line of code, it should wait for whatever command you ran before to finish.
This would not be ideal if you are running scripts in the same window or other windows in the background (for instance, you may run a command in the background and continue using the current window for other commands if the first command is followed by an ampersand); be careful!
If you wrap the osascript code in a shell script file, you can probably call it with whatever pithy file-name you give it---as long as it is in Terminal's search path (run echo $PATH to see where Terminal looks for scripts).
I've been using
quit -n terminal
at the end of my scripts. You have to have the terminal set to never prompt in preferences
So Terminal > Preferences > Settings > Shell
When the shell exits
Close the window
Prompt before closing
Never
Create a script:
cat ~/exit.scpt
like this:
Note: If there is only one window, just quit the application, else simulate command + w to close the tab)
tell application "Terminal"
set WindowNum to get window count
if WindowNum = 1 then
quit
else
tell application "System Events" to keystroke "w" using command down
end if
end tell
Then add a alias in your *shrc
just like vi ~/.bashrc or zshrc (anything else?)
add it:
alias exit="osascript ~/exit.scpt"
And source the ~/.bashrc or reopen your terminal.app
This is what I did for a command I just wrote. I wanted to be able to create a "shortcut" to the Backup directory for Apple Configurator that worked on different computers, but since it's relative to the user's home directory, I needed to make it a .command file. Here are the contents:
#!/usr/bin/env bash
open "${HOME}/Library/Application Support/MobileSync/Backup"
(sleep 0.1 ; osascript -e 'tell application "Terminal" to quit') &
I tried several variations of the answers here. No matter what I try, I can always find a use case where the user is prompted to close Terminal.
Since my script is a simple (drutil -drive 2 tray open -- to open a specific DVD drive), the user does not need to see the Terminal window while the script runs.
My solution was to turn the script into an app, which runs the script without displaying a Terminal window. The added benefit is that any terminal windows that are already open stay open, and if none are open, then Terminal doesn't stay resident after the script ends. It doesn't seem to launch Terminal at all to run the bash script.
I followed these instructions to turn my script into an app: https://superuser.com/a/1354541/162011
If this is a Mac you type 'exit' then press return.
I can created an (very) simple applescript app to run Firefox background & exit. (The reason is I have different profiles for work & home). My script is basically:
do shell script
"/Applications/firefox.app/Contents/MacOS/firefox
-no-remote -P 'Personal' &"
It works, but the script/app doesn't exit until I quit Firefox. How can I fix that?
You need to redirect stdout and stderr somewhere. The do shell script command knows that the pipes it setup for the program's stdout and stderr are still open, so it waits for them to be closed.
do shell script "/Applications/firefox.app/Contents/MacOS/firefox -no-remote -P 'Personal' > /dev/null 2>&1 &"