How to run .bash file in Mac? - bash

I want to execute
Path : "/Users/Trans/Downloads/solr-6.1.0"
Command: "bin/solr start -p 8983"
and
Path : " /Users/Trans/Downloads/apache-activemq-5.12.0/bin"
Command: "./activemq console"
To make it executable on click I create one .command file
having
!#/bin/bash
"/Users/Trans/Downloads/solr-6.1.0" bin/solr start -p 8983
"/Users/Trans/Downloads/apache-activemq-5.12.0/bin" ./activemq console
Given chmode +x permissions to it
But something seems to be wrong, Code is not getting executed.
My aim is to create one batch/bash file on double clicking on it should open terminal and execute both commands or more than that.

Shebang begins with #! not !#
#!/bin/bash
# verbose mode ( or -v in shebang )
set -v
cd "/Users/Trans/Downloads/solr-6.1.0" && bin/solr start -p 8983
echo "press a key to continue"
read -n1
cd "/Users/Trans/Downloads/apache-activemq-5.12.0/bin" && ./activemq console
echo "press a key to continue"
read -n1

Related

How to get PID of terminal inside script which is created gnome-terminal command, but keeping terminal active for later use

Here is my command
gnome-terminal --working-directory ~ -- bash -c " echo 'Execution started' && ./test && echo 'Execution finished' ; /bin/bash"&
#!/bin/bash
echo "PPID ="
echo $PPID
I use in the end of my command /bin/bash to create new terminal and keep it opened for later use, but I need to store PID of terminal which is runs my script and not close it, gnome-terminal command always closed terminal which executes my ./test script
I understand, you want the PID of the terminal where you lauch a new terminal.
So:
gnome-terminal --working-directory ~ -- bash -c "FATHER_TERM_PROC=$PPID /bin/bash" &
In the new terminal:
$ echo PID=$$, PPID=$PPID, FATHER_TERM_PROC=$FATHER_TERM_PROC
PID=5166, PPID=5159, FATHER_TERM_PROC=4417
$ ps -o pid,ppid,args -p $$ -p $PPID $FATHER_TERM_PROC
PID PPID COMMAND
4417 2043 /usr/bin/xfce4-terminal --geometry=125x40
5159 1600 /usr/libexec/gnome-terminal-server
5166 5159 /bin/bash
Now, you known, my first terminal is a XFCE terminal!
Is this what you wanted?

WSL run linux from windows without spawning a cmd-window

I have WSL bash running in a cmd. I don't use it for anything, it just hangs there to keep the WSL system alive.
When I start X applications:
bash -c "DISPLAY=:0 xmessage hello &"
I get this result:
I can close down the command window without any problems, but it's rather annoying.
How can run commands without getting this cmd window every time?
Here's a simpler solution, which, however, requires a WSH-based helper script, runHidden.vbs (see bottom section):
wscript .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world'"
To apply #davv's own launch-in-background technique to avoid creating a new bash instance every time:
One-time action (e.g., at boot time): launch a hidden, stay-open bash window. This spawns 2 bash processes: the Windows bash.exe process that owns the console window, and the WSL bash process (owned by the WSL init singleton), which is then available for servicing background commands.
wscript .\runHidden.vbs bash # hidden helper instance for servicing background commands
For every X Window-launching command: Terminate each command with & to have it be run by the hidden WSL bash instance asynchronously, without keeping the invoking bash instance alive:
wscript .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world' &"
runHidden.vbs source code:
' Simple command-line help.
select case WScript.Arguments(0)
case "-?", "/?", "-h", "--help"
WScript.echo "Usage: runHidden executable [...]" & vbNewLine & vbNewLine & "Runs the specified command hidden (without a visible window)."
WScript.Quit(0)
end select
' Separate the arguments into the executable name
' and a single string containing all arguments.
exe = WScript.Arguments(0)
sep = ""
for i = 1 to WScript.Arguments.Count -1
' Enclose arguments in "..." to preserve their original partitioning, if necessary.
if Instr(WScript.Arguments(i), " ") > 0 then
args = args & sep & """" & WScript.Arguments(i) & """"
else
args = args & sep & WScript.Arguments(i)
end if
sep = " "
next
' Execute the command with its window *hidden* (0)
WScript.CreateObject("Shell.Application").ShellExecute exe, args, "", "open", 0
Even when launched from a GUI app (such as via the Run dialog invoked with Win+R), this will not show a console window.
If your system is configured to execute .vbs scripts with wscript.exe by default (wscript //h:wscript /s, which, I think, is the default configuration), you can invoke runHidden.vbs directly, and if you put it in your %PATH%, by filename (root) only: runHidden ....
Note that use of the script is not limited to console applications: even GUI applications can be run hidden with it.
There's another simple solution, it requires an external executable though. It has no dependencies and was recommended by aseering on GitHub.
you can launch bash via run.exe: run.exe bash.exe -c "<whatever Linux command>". (run.exe is available here: http://www.straightrunning.com/projectrun/ , make sure you download the 64-bit version, the 32-bit version will not be able to find or run bash).
With run on the search PATH, you can just call
run bash -c "DISPLAY=:0 xmessage hello"
So I just made this workaround for now. I really hope that there's a better way than this, but here it goes:
In the command prompt that lives purely to keep WSL alive, I have this script running:
wsl_run_server
#!/bin/bash
set -e
nc -kl 127.0.0.1 15150 | sh
And then I have this command to execute commands in background:
wsl_run_command
if ! pidof -x bin/wsl_run_server; then
echo wsl_run_server isnt running!
exit 1
fi
echo \($#\) \& | nc localhost 15150
from windows I then call:
bash -c "DISPLAY=:0 ~/bin/wsl_run_command xmessage hello"
There no longer is a need to have that command window pop up anymore with WSLg recently added to the mix. You just can call bash using WSLg, like so (I use Ubuntu currently in WSL):
wslg ~ -d Ubuntu bash
This will create a BASH session that will just sit there without being seen. Alternatively, you can do what I do and run a few services that stay running. I created a script that checks for running services, and if it doesn't find them running, will run them. Create the file in /usr/bin:
sudo touch /usr/bin/start-wsl-services
sudo nano /usr/bin/start-wsl-services
Past the following into the file:
#!/bin/bash
# Check for and run System-wide DBus service.
SERVICE="dbus-daemon"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/dbus start
pgrep -a "$SERVICE"
fi
# Check for and run CUPS Printing Service.
SERVICE="cupsd"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/cups start
pgrep -a "$SERVICE"
fi
# Check for and start Freshclam CLAMAV Update service.
SERVICE="freshclam"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/clamav-freshclam start
pgrep -a "$SERVICE"
fi
# Check for and start SANED Scanner service.
SERVICE="saned"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/saned start
pgrep -a "$SERVICE"
fi
# Check for and start screen-cleanup service.
SERVICE="screen-cleanup"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/screen-cleanup start
pgrep -a "$SERVICE"
fi
# Check for and start Preload service.
SERVICE="preload"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/preload start
pgrep -a "$SERVICE"
fi
# Prestart LibreOffice twice for faster loading.
#/usr/bin/libreoffice --terminate_after_init
#sleep 5
#/usr/bin/libreoffice --terminate_after_init
# Check for error, make sure all functions called and run, and pass the result on to calling process.
if [[ $? -ne 0 ]] ; then
exit 1
else
exit 0
fi
Save and exit the file, and then make it executable:
sudo chmod +x /usr/bin/start-wsl-services
I then call this using a shortcut that runs a startup script at startup. Or you can just run it manually. The command I use in the startup script is:
C:\Windows\System32\wslg.exe -d Ubuntu -- /usr/bin/start-wsl-services
The startup command script I use (named StartWSL.cmd) is as follows:
#echo off
echo Starting WSL Linux...
:RETRY
C:\Windows\System32\wslg.exe -d Ubuntu -- /usr/bin/start-wsl-services
REM - C:\Windows\System32\bash.exe -c '/usr/bin/start-wsl-services'
IF %ERRORLEVEL% NEQ 0 (GOTO RETRY)
REM - Allow time to see all results.
timeout /t 5 /nobreak >NUL
REM - Uncomment below line for troubleshooting.
REM - pause
exit 0
And that's how I now keep WSL running in the background on Windows 11, and similar to how I did it on Windows 10.
run command background
screen -dmS [name] [command]
example
screen -dmS gui bash -c "DISPLAY=:0 xmessage hello"
create a shortcut on windows desktop(run in wsl)
wslusc screen -dmS gui bash -c "DISPLAY=:0 xmessage hello"

Guake terminal startup shell script

I'm trying to create a startup script to open two tabs in Guake, execute commands, and then rename the tabs. Unfortunately the tabs are not being renamed. Also irritating, it's opening up the terminal in the directory of the shell script instead of the default ~/ directory. After reading the help file, I'm pretty sure that the script is correct.
Help file:
Options:
-h, --help show this help message and exit
-f, --fullscreen Put Guake in fullscreen mode
-t, --toggle-visibility
Toggles the visibility of the terminal window
-p, --preferences Shows Guake preference window
-a, --about Shows Guake's about info
-n NEW_TAB, --new-tab=NEW_TAB
Add a new tab
-s SELECT_TAB, --select-tab=SELECT_TAB
Select a tab
-g, --selected-tab Return the selectd tab index.
-e COMMAND, --execute-command=COMMAND
Execute an arbitrary command in the selected tab.
-r RENAME_TAB, --rename-tab=RENAME_TAB
Rename the selected tab.
-q, --quit Says to Guake go away =(
And here is my shell script:
#!/bin/sh
# guakeStartup.sh
# open tabs
guake -e "irssi" -r "irssi"
guake -n -e "cd" -r "terminal"
Try it like this inside your startup script:
guake --rename-tab="irssi" --execute-command="irssi" &
sleep 1 &&
guake --new-tab=2 --rename-tab="terminal" --execute-command="cd" &
Or more like your original:
guake -e "irssi" -r "irssi" &
sleep 1 &&
guake -n=2 -e "cd" -r "terminal" &

exo-open in a terminal script - program closes with the terminal

I've written a simple bash script that prompts for a file or directory path and opens it with exo-open, I've then assigned the script to a keyboard shortcut so that I can CTRL+SHIFT+ALT+O to open anything at anytime via a terminal prompt:
And the script:
#!/bin/bash
# CD to the home folder (not sure if this is needed, no harm either way)
cd ~/
# Request the filepath
echo -e "\e[1;31mEnter a file or directory:\e[00m"
read -e -i "~/" filename
# Convert ~/ to /home/username/
filename=`eval "echo $filename"`
echo -e "opening\e[1;32m" $filename "\e[00m"
# Open the file
exo-open "$filename"
echo "press enter to exit"
read enter
My problem is that the spawned program is linked to the terminal, when the terminal closes it takes the program with it - as a simple workaround I have another user prompt at the end to stop the terminal from closing; does anyone know how I could allow the terminal to close but keep the resulting program open?
Some ideas I've had / tried:
run disown $! after exo-open (didn't work)
use nohup (didn't work)
Run exo-open from the PPID (no idea how to do this)
At my wits end :-(
I had this answered by Xfce forum member ToC
http://forum.xfce.org/viewtopic.php?pid=25670
Turns out you can use setsid like so:
#!/bin/bash
# CD to the home folder (not sure if this is needed, no harm either way)
cd ~/
# Request the filepath
echo -e "\e[1;31mEnter a file or directory:\e[00m"
read -e -i "~/" filename
# Convert ~/ to /home/username/
filename=`eval "echo $filename"`
echo -e "opening\e[1;32m" $filename "\e[00m"
# Open the file
setsid exo-open "$filename"

Execute script inside another exits outer script

I try to launch another app inside a bash script, but the app seems to exit my script so that the line exec $HOME/bin/sync-iosbeta; does not get executed. I have tried to put it outside the if as well.
if $HOME/bin/BetaBuilder.app/Contents/MacOS/BetaBuilder --args -i "${zip}" -o "${odir}" -u "${ourl}" -r "$PROJECT_FOLDER/README.txt" ; then
echo "Wil sync"
exec $HOME/bin/sync-iosbeta;
fi
echo "This text does not get printed either..";
I have also tried to use open to kick off the app, but then I have issues with passing the arguments, even with --args set.
I am running on Mac OS.
From the exec manual:
If command is specified, it replaces the shell. No new process is created.
Just remove exec and the ";":
if $HOME/bin/BetaBuilder.app/Contents/MacOS/BetaBuilder --args -i "${zip}" -o "${odir}" -u "${ourl}" -r "$PROJECT_FOLDER/README.txt" ; then
echo "Wil sync"
$HOME/bin/sync-iosbeta
fi
echo "This text does not get printed either..";
If sync-iosbeta is not being executed, then may be it hasn't the right permissions. Try:
if $HOME/bin/BetaBuilder.app/Contents/MacOS/BetaBuilder --args -i "${zip}" -o "${odir}" -u "${ourl}" -r "$PROJECT_FOLDER/README.txt" ; then
echo "Wil sync"
/bin/sh $HOME/bin/sync-iosbeta
fi
echo "This text does not get printed either..";
That's the whole point of exec. man bash: If command is specified, it replaces the shell.
just remove exec.

Resources