Determine which of running servers, an emacsclient attached to? - emacs

I planed to starting emacs from the the start.sh as
$ head start.sh
#! /bin/bash
{
#starting emacs servers
emacs --daemon=orging
emacs --daemon=coding
#waiting...
#invoke emacsclients
emacsclient -c -s "orging" &
emacsclient -c -s "coding" &
......
} &> /dev/null
Two clients run respectively under servers of orging and coding.
A problem occurred to this situation is that the invoked running clients are not labelled with appropriate server names.
So a manual steps of testing might be need to determine who is who.
As an alternative, the servers could be scheduled with one running at the top , the other at the end after starting from in the start.sh,
How could determine which server a client attached in a straightforward way on a working frame?

You can inspect the variable server-name - interactively with C-h v server-name RET.

Related

execute buffer content in current shell upon exec ssh

My perl code does this
#!/usr/bin/perl
sleep 2;
exec 'ssh', '-o', "ConnectTimeout=10", "newhost", "sleep 3;pwd";
the problem is when sleep is executing what ever I type on the terminal (during execution)
disappears to non interactive shell in newhost.
eg:
user#a02$perl test.pl
ls
user#a02$ #ls is not executed
command ls executes if I don't use exec or system.
Is there a way to execute the contents of the buffer?
After a bit of digging i found ssh has has an stdio redirector when used with -n option.
http://www.pixelbeat.org/programming/stdio_buffering/
To tell ssh that the remote command doesn't require any input use the -n option

Determine if the stdout is terminal under ssh

To test whether the output is terminal we can do -t STDOUT:
if (-t STDOUT) {
# print with terminal control chars
} else {
# just plain print
}
But when the script is executed in the ssh session not run from terminal (Jenkins in my case), the -t test still returns true and my output gets polluted with control chars:
ssh user#server "/my/script.pl"
Why does the -t detects the terminal?
I don't know why ssh is allocating a terminal for you — mine defaults to not doing that even if the output of ssh goes to a terminal — but passing -T to ssh will disable pseudo-tty creation on the remote end.
$ ssh -t localhost "perl -E'say -t STDOUT ?1:0'"
1
Connection to localhost closed.
$ ssh -T localhost "perl -E'say -t STDOUT ?1:0'"
0
From ssh's man page:
-T Disable pseudo-tty allocation.
-t Force pseudo-tty allocation. This can be used to execute arbitrary
screen-based programs on a remote machine, which can be very useful,
e.g. when implementing menu services. Multiple -t options force tty
allocation, even if ssh has no local tty.
Perhaps it would be better if you instead forced ssh to allocate a pty —
From the ssh manual:
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs
on a remote machine, which can be very useful, e.g. when implementing menu services.
Multiple -t options force tty allocation, even if ssh has no local tty.
The longer answer: -t (the Perl or Bourne shell function) reliably detects whether the stream is a “typewriter,” but ssh will normally only allocate a pseudo-teletype (pty) stream in interactive shells, not when other programs are being started.
See also RequestTTY as an option in .ssh/config.

How do you stop a perl Dancer/Starman/Plack server?

I started a Dancer/Starman server using:
sudo plackup -s Starman -p 5001 -E deployment --workers=10 -a mywebapp/bin/app.pl
but I'm unsure how I can stop the server. Can someone provide me with a quick way of stopping it and all the workers it has spawned?
Use the
--pid /path/to/the/pid.file
and you can kill the process based on his PID
So, using the above options, you can use
kill $(cat /path/to/the/pid.file)
the pid.file simply stores the master's PID - don't need analyze the ps output...
pkill -f starman
Kill processes based on name.
On Windows you can do "CTRL + C" like making a copy but Cancel in this case. Tested working.

perl run two system commands error

So in my script I need to make to calls to unix, and I do it via the system command like so:
system "bash -i -c 'addmothernode'";
...
perl code ...
...
system "bash -i -c 'addnode -ip=$_'";
However, whenever I run both of these commands in the same script, for some reason my process is stopped like this:
[1]+ Stopped perl boot.pl
And the script can only be finished when I run fg %1. When I only have one of these system calls in, the perl script finishes successfully. But I need both commands because they depend on each other. Anyone have any ideas about what's going on? Thanks!
UPDATE:
A lot of answers below are saying I don't need to use bash -i to run a system command, and I know typically this is true but I need to use aliases that I have created and if I do not use this the aliases won't be recognized. So I do need bash -i.
This problem is unrelated to perl. You can easily reproduce the situation if you start two bashes in the interactive mode (-i) one after another:
$ cat 1.sh
bash -i -c 'sleep 1'
bash -i -c 'sleep 1'
$ bash 1.sh
[1]+ Stopped bash 1.sh
Of course it would be better to run bash in the non-interactive mode (without -i) or run the program directly, without bash, but if you need for some reason bash -i you can protect its run with setsid:
$ cat 1.sh
setsid bash -i -c 'sleep 1'
setsid bash -i -c 'sleep 1'
echo done
$ bash 1.sh
done
The bash -i means run an interactive shell; so you have two shells both reading from the terminal.
Try removing the -i options.
system "addmothernode";
should work.
To execute a command, bash is not needed. The Perl system function is like the system C function, it calls by default sh.
man system
exec
The standard to which the caller conforms determines which shell is used. See standards(5).
Standard Shell Used
______________________________________________________________
1989 ANSI C, 1990 ISO C, 1999 ISO C, /usr/xpg4/bin/sh
POSIX.1 (1990-2001), SUS, SUSv2, SUSv3,
XPG4
POSIX.1 (1988), SVID3, XPG3, no standard /usr/bin/sh
specified

Invoking su in emacs' eshell

I can run normal linux commands in eshell as a non-root user. When I try to su to root, all the usual linux commands are gone (ls, cat, etc.) How do I fix this?
One way is to use tramp:
cd /su::/etc/
ls
then eshell will run command "remotly" using su.
After testing, it seem that ls do work, but there is a problem with some (but not all) external command.