Interacting with an existing terminal using Expect - expect

Is it possible to interact with an already existing terminal using Expect?
Usually a process/terminal is launched via the Expect script, but I want to interact with a terminal that was opened not by the Expect script.

Not without using other tools, like "screen", and attaching to the other shell.
An example of this:
$ screen bash
Now you could, from another teminal, attach to the same shell and do stuff, using this:
$ screen -x
So i guess your Expect could do the same, issuing "screen -x" and then doing stuff on the shell.

Related

How can I make GNU screen run a command or script every time I create a new window?

I'm trying to write a script that makes GNU screen call virtualenvwrapper's workon command to jump to an existing virtualenv based on the screen session name.
I've managed to make it work when the session starts, but I want to be able to run commands everytime a window is created, and I can't find hooks or anything similar to it.
Does screen allow a way to run commands everytime a window is created within a session?
Try putting the logic into your ~/.profile (if using the deflogin on setting) or ~/.mkshrc (or similar, depending on your shell) file. Something like this:
if test -n "$STY"; then
# we are inside GNU screen
screenpid=${STY%%.*}
screenname=${STY#*.}
# do your magic
fi
In this scenario, $screenname will contain either the name passed to the -S option of GNU screen, or something like ttyp0.shorthostname or pts-9.shorthostname (depending on the OS) if -S was not used.

In Bash, how can I tell if I am currently in a terminal

I want to create my own personal logfile that logs not only when I log in and out, but also when I lock/unlock my screen. Kindof like /var/log/wtmp on steroids.
To do this, I decided to run a script when I log into Ubuntu that runs in the background until I quit. My plan to do this is to add the script to .bashrc, using ./startlogging.sh & and in the script I will use trap to catch signals. That's great, except .bashrc gets run every time I open a new terminal, which is not what I want for the logger.
Is there a way to tell in Bash that the current login is a gnome login? Alternatively, is there some sort of .gnomerc I can use to run my script?
Edit: Here is my script:
Edit 2: Removed the script, since it's not related to the question. I will repost my other question, rather than repurpose this one.
Are you looking for a way to detect what type of terminal it is?
Try:
echo $TERM
From Wikipedia:
TERM (Unix-like) - specifies the type of computer terminal or terminal
emulator being used (e.g., vt100 or dumb).
See also: List of Terminal Emulators
for bash use : ~/.bash_logout
that will get executed when you logout, which sounds like what you are trying to do.
Well, for just bash, what you want are .bash_login/.bash_logout in your home directory (rather than .bashrc) These are run whenever a LOGIN shell starts/finishes, which happens any time you log in to a shell (on a tty or console, or via ssh or other network login). These are NOT run for bash processes created to run in terminal windows that you create (as those are not login shells) so won't get run any time you open a new terminal.
The problem is that if you log in with some mechanism that does not involve a terminal (such as gdm running on the console to start a gnome or kde or unity session), then there's no login shell so .bash_login/logout never get run. For that case, the easiest is probably to put something in your .xsessionrc, which will get run every time you start an X session (which happens for any of those GUI environments, regardless of which one you run). Unfortunately, there's no standard script that runs when an X session finishes.

Running a command as a background process/service

I have a Shell command that I'd like to run in the background and I've read that this can be done by suffixing an & to the command which causes it to run as a background process but I need some more functionality and was wondering how to go about it:
I'd like the command to start and run in the background every time the system restarts.
I'd like to be able to able to start and stop it as and when needed just like one can do service apache2 start.
How can I go about this? Is there a tool that allows me to run a command as a service?
I'm a little lost with this.
Thanks
UNIX systems can handle as many processes as you need simultaneously (just open new shell windows if you're in a GUI), so running a process in the background is only necessary if you need to carry on using the current shell window for other things once you've run an application or process that keeps running.
To run a command called command in background mode, you'd use:
command &
This is a special character that returns you to the command prompt once the process is started. There are other special characters that do other things, more info is available here.
Take a look at the daemon command, which can turn arbitrary processes into daemons. This will allow your script to act as a daemon without requiring you to do a lot of extra work. The next step is to invoke it automatically at boot. To know the correct way to do that, you'll need to provide your OS (or, for Linux, your distribution).
Based on this article:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/
...another good way is with screen eg:
screen -d -m -s "my session name" <command to run>
from the screen manual:
-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
i.e. you can close your terminal, the process will continue running (unlike with &)
with screen you can also reattach to the session later
Use nohup while directing the output to /dev/null
nohup command &>/dev/null &
For advanced job control with bash, you should look into the commands jobs bg and fg.
However, it seems like you're not really interested in running the command in the background. What you want to do is launch the command at startup. The way to do this varies depending on the Unix system you use, but try to look into the rc family of files (/etc/rc.local for example on Ubuntu). They contain scripts that will be executed after the init script.

Using AppleScript to Send Commands and Strings to Terminal.app with Echo Off

what I want to accomplish is something like the one described in this this question. Basically using AppleScript to send commands to the Terminal.app.
However there's this behavior that I don't want: every command sent using do script directive is echoed to the Terminal. I am currently integrating an AppleScript with Cocoa, and sometimes the software would send sensitive information such as password to the Terminal.
Is there some way to disable this behavior, such as #echo off directive in DOS batch files?
EDIT
To clarify my question, I will elaborate more. Suppose we have an AppleScript such as this one:
tell application "Terminal"
set currentTab to do script "login"
do script "username" in currentTab
do script "password" in currentTab
end tell
I noticed that if the Terminal application is already running, with or without any terminal window open, the commands in the do script directive will be echoed before it is fed to the shell. To illustrate the result of the above script in a Terminal:
Last login: Tue 5 Apr hh:mm:ss on ttys001
login <--\
username <----unwanted echoes
password <--/
<machine>:~ <user>$ login
username: username
password: ****
... (interactive Terminal session)
This doesn't happen however, if the Terminal.app is not running at script execution.
You're "typing" things before the shell has a chance to respond to them (in this case, before login has a chance to turn off echoing). Tools such as expect solve the problem of scripting arbitrary command line utilities, which would be a better solution in the general case, but it's not clear from your question what you're trying to do.
What command are you trying to script, and why are you doing it via Terminal?
To hide the commands in Terminal.app first run this command:
stty -echo
To show the commands again:
stty echo
Instead of using Terminal.app you can also run commands directly from AppleScript:
set theResult to do shell script "cal -y 2011"
Or even better, run the commands directly from Objective-C with NSTask.
(Since your app is using Cocoa I assume it has been (partially) written in Objective-C)

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