How do I autorun an application in a terminal in Ubuntu? - bash

I've created a few autorun script files on various USB devices that run bash scripts when they mount. These scripts run "in the background", how do I get them to run in a terminal window? (Like the "Application in Terminal" gnome Launcher type.)

Run them as a two stage process with your "autorun" script calling the second script in a new terminal eg
gnome-terminal -e top --title Testing
Would run the program "top" in a new gnome terminal window with the title "Testing" You can add additional arguments like setting the geometry to determine the size and location of the window checkout the man page for gnome-terminal and the "X" man page for more details

xterm -e shellscript.sh
or (if xterm isn't installed)
gnome-terminal -e shellscript.sh
or (if you're using kubuntu / kde)
konsole -e shellscript.sh

Related

Open a Graphical Window that uses Ncurses

Let's say I'm running xterm or some other terminal emulator on top of some graphical X Window Environment. xterm on KDE, for example. I want to write an application that may be called on the command line of one terminal emulator, and upon execution, the application creates a new KDE/Environment window, separate from the current terminal emulator, and on that new window run an Ncurses context for application purposes.
How would I do this? Is this terminal emulator dependent? Desktop environment dependent? Would I be using bare X window system calls?
It's terminal (emulator) dependent.
Curses runs in terminals and terminal emulators. Most of the latter (terminal emulators) have some way to tell what program (other than your shell) to run in the terminal emulator.
For instance, xterm can be told using the -e option,
-e program [ arguments ... ]
This option specifies the program (and its command line
arguments) to be run in the xterm window. It also sets the
window title and icon name to be the basename of the program
being executed if neither -T nor -n are given on the command
line.
NOTE: This must be the last option on the command line.
letting you start an xterm running MyApplication like this:
xterm -e MyApplication
Again, most of the terminal emulators you might run in X accept that option. KDE konsole does that. There's no manual, but
konsole --help-all
shows it at the end.

Launch Emacsclient with GUI (from Dock) on Mac OS X

How can I launch Emacsclient with GUI from the Dock (or maybe also from the terminal) on Mac OS X?
The EmacsWiki describes how to create an "Emacs from Dock" app using Automator. It worked for me but I don't want to launch Emacs but Emacsclient. So, I tried replacing /Applications/Emacs.app/Contents/MacOS/Emacs with both /Applications/Emacs.app/Contents/MacOS/bin/emacsclient and /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c but both didn't work.
From the terminal
You can find the appropriate path to emacsclient using type in your shell (assuming emacsclient -c works from said shell):
$ type emacsclient
emacsclient is /usr/local/bin/emacsclient
Then we can add the appropriate emacsclient flags (see $ man emacsclient for details) to open the GUI:
/usr/local/bin/emacsclient -n -c -a ""
From macOS GUI
To launch emacsclient from eg the Dock or Spotlight, it's easy to use Automator. Automator is built in to macOS.
Choose to make an "Application", then choose "Run Shell Script", and add a modified version of the above call to emacsclient:
/usr/local/bin/emacsclient -n -c -a "" -- "$#"
Then change "Pass input": use "as arguments" instead of "to stdin".
The added "$#" is where any optional arguments passed to this shell script will be placed. Here, this allows you to pass a filename to open with emacsclient. Automator automates passing this filename in when, eg, you click to open a file with the application we've just made. This also allows you to set the application to be the default application for certain file types.
From anywhere, flexibly
Another way to run the above shell command is with skhd (link). skhd is far more involved to learn, but ultimately makes it much easier to set up a large number of shell commands with rapid access.
For example, you could make "Ctrl-o" from anywhere in macOS enter a mode you name open_app, from which you could press "e" to open emacsclient, "d" to open emacs --debug-init, "t" to run emacs --adv-timers, "f" to open Firefox, "F" to open a second Firefox profile, etc.
One idea would be to create an applescript that does whatever the original poster desires, and wrap it up in an application using something like platypus or automator. See https://superuser.com/questions/685111/basic-setup-of-emacs-server-under-osx for additional ideas such as using the --daemon command-line argument instead of placing (server-start) within the user-configuration file.
Here is an example applescript:
# (server-start) must be inside `init.el` or `.emacs` file.
#
# This script can also be used in the terimal: osascript path-to-script arguments
# Terminal Example:
# osascript /absolute/path/to/applescript/file "-e '(progn (dired \"/Applications\") (message \"Hello-World\!\"))'"
on run argv
set arg to item 1 of argv
set emacs to application "Emacs"
set appIsRunning to emacs is running
if appIsRunning then
say "Emacs is already running."
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
else
tell application "/Applications/Emacs.app/Contents/MacOS/Emacs" to activate
say "Please wait five seconds for Emacs to load."
delay 5
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
end if
end run

How to create a shell script to launch 3 terminals and execute a set of commands in each?

Currently, I open 3 terminals ('openocd session','telnet session' & 'gdb session') and execute 'a set of commands' in each terminal every time I flash my image on target.
Is is possible to put this in one shell script file and run at once? (Commands on each terminal are NOT dependent on others; except that the terminals should be opened in the order mentioned above)
Thanks in advance !
Which terminal are you using?
A terminal like xterm can start a program instead of a shell. Simply run
xterm -e 'command args ...'
I use this to start my email client:
xterm -e mutt
If you use a different terminal, consult its manual page. I'm pretty sure there's an equivalent for -e.

Mac OS X: Bring (non-bundle) GUI applications to foreground when launched from the command line

When a GUI process is launched from the OS X terminal, the window shows up in the background, and you have to use command-tab to give it focus.
Is there a way to make the terminal automatically give such GUIs focus after they are launched?
For example (assuming gitk is installed):
% gitk
should launch the GUI and then switch to it.
Note: For several reasons, using open as this answer suggests is not a general solution.
Update: To better explain why the open method isn't satisfactory, here's a sample bash session (with witty commentary).
% cd /my_repo
% gitk
Waiting for the GUI to appear ... any day now ... oh wait -- it's already open. I just didn't notice because it opened a window BEHIND my terminal. I wonder how long I was going to sit here waiting....
% open gitk
The file /my_repo/gitk does not exist.
Ah, of course.
% which gitk
/usr/bin/gitk
% open /usr/bin/gitk
What the ... it opened a new terminal window to run gitk, and it did so in my home directory, not /my_repo, so gitk complains that the current directory isn't actually a repository...
Do you need to invoke it synchronously? If not, you could start it asynchronously with & and then activate it with osascript -e 'tell application "gitk" to activate'.
If you are dealing with gitk specifically you can edit the gitk file to achieve this, I posted an answer on the apple stack exchange: https://apple.stackexchange.com/a/74917/35956
You can find the gitk file using the following command from the terminal
which gitk
In my gitk file I found a line that looks like the following near the top (line 3)
exec wish "$0" -- "$#"
I changed it to this
exec wish "$0" -- "$#" & exec osascript -e "tell application \"Wish\" to activate"
When I execute gitk from the command line the gitk window comes to the foreground, another side effect of this is that it executes asynchronously
You can wrap up #chris page's answer in a bash function and drop it in your .bashrc
function gitk() {
command gitk "$#"&
command osascript -e "delay .5" -e "tell application \"wish\" to activate"
}
There should be a way to get rid of the delay by looping and looking for 'wish' with a timeout.
NOTE: 'Wish' is the window title that shows up on my Mac for gitk.

Linux equivalent of the DOS "start" command?

I'm writing a ksh script and I have to run a executable at a separate Command Prompt window.
xdg-open is a similar command line app in linux.
see https://superuser.com/questions/38984/linux-equivalent-command-for-open-command-on-mac-windows for details on its use.
I believe you mean something like xterm -e your.sh &
Don't forget the final &
maybe it´s not a seperate window that gets started, but you can run some executables in background using "&"
e.g.
./myexecutable &
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for.
regards
xdg-open is a good equivalent for the MS windows commandline start command:
xdg-open file
opens that file or url with its default application
xdg-open .
opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
screen -ls
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
screen -x title
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this:
xterm yourtextmodeprogram
or
gnome-terminal -e yourtextmodeprogram
or
konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this:
batch-oriented: nohup command-to-run &
interactive: screen
if you want a new windows, just start a new instance of your terminal application: in kde it's
konsole -e whatever
i'm sure the Gnome terminal has similar options
Some have recommended starting it in the background with &, but beware that that will still send all console output from the application you launch to the terminal you launched it from. Additionally, if you close the initial terminal the program you loaded will end.
If you're using a desktop environment like KDE or GNOME, I'd check the alt+f2 launching apps (gnome-open is the one for GNOME, I don't know the name of the KDE app) and see if you can pass them the command to launch as an argument.
Also, if your intention is to launch a daemon, you should check the nohup documentation.
I used nohup as the following command and it works:
nohup <your command> &
then press enter and enter!
don't forget the last &
for example, I ran a python code listening to port 5000:
nohup python3 -W ignore mycode.py &
then I made sure of running by netstat -tulnp | grep :5000 and it was ok.

Resources