WSL, Running linux commands with "wsl --exec <cmd>" or "wsl -- <cmd>" - linux

wsl -h shows the following:
--exec, -e <CommandLine> Execute the specified command without using the default Linux shell.
-- Pass the remaining command line as is.
What does "without using the default Linux shell" mean (i.e. what else is it going to use, if not the default shell!?)?.
Additionally, by way of an example, I now have three possible ways to run Linux ls from my PowerShell prompt (i.e. this will not be Get-ChildItem aliased to ls, but instead a Linux command via WSL):
PS C:\> wsl -e ls # Alternatively, wsl --exec ls
PS C:\> wsl -- ls
PS C:\> wsl ls
But all outputs appear to be the same. How would you explain the differences between these three ways of running a WSL Linux command from a PowerShell prompt?

I think it means wsl runs the command directly, instead of spawning a shell process to run the command.
For example, if I run :
wsl -e sleep 10
From another terminal, I have :
root 1482 1 0 11:32 tty3 00:00:00 /init
ubuntu 1483 1482 0 11:32 tty3 00:00:00 sleep 10
We can see /init is the parent of sleep 10, without a shell in between.

A cool trick is using this to set the X11 $DISPLAY variable, letting you use windows terminal to get remote shells using WSLG.
# in microsoft terminal or powershell use this command line
wsl.exe -- ssh -a -X -Y $hostname
then on the remote system
# DISPLAY will show something like localhost:10.0 on the remote system
echo $DISPLAY
# use a program like xeyes to test
xeyes

Related

How to distinguish between two running Linux scripts with the same name?

I use SSH to connect to Linux, maybe run a Linux script multiple times, and use nohup to suspend these processes, and then close the SSH connection. After the next SSH connection to Linux, how can I distinguish between different scripts and get different PIDs?
This Linux script will always print content on the screen. I use Python's paramiko library, SSH to Linux, run the script and use nohup to suspend the process and redirect the output to the file. This process may be multiple times. How to distinguish the starting process, find its PID and kill it. It is best not to modify the Linux script because the script is not written by me.
I use the script name to find the process number, get a lot of PIDs, I can't distinguish them.
You could parse the output of ps -eo pid,lstart,cmd which shows the process id, start time and path, e.g.:
PID STARTED CMD
1 Mon Jun 19 21:31:08 2017 /sbin/init
2 Mon Jun 19 21:31:08 2017 [kthreadd]
3 Mon Jun 19 21:31:08 2017 [ksoftirqd/0]
== Edit ==
Be aware that if the remote is macOS the ps command does not recognize the cmd keyword, use comm or command instead, e.g.: ps -eo pid,lstart,comm
Use ps command to check running process.
For checking only shell scripts , You an do something like this:-
ps -eaf |grep .sh
This will give you all the information about running shell scripts only, easily you can distinguish b/w running scripts.
In place of ".sh" you can give file name also, than you will get information only about that running file.
maybe change the command you run to do something like:
nohup command.sh &
echo "$! `date`" >> runlog.txt
wait
i.e. run the command in the background, append its PID to a log (you might want to include more identifying information here or use a format that's more easily machine readable), then wait for it to finish
another variant would be to run tmux (GNU screen) on the server and run commands in an existing session:
tmux new-window command
which would also let you "reconnect" to running scripts later to check output / terminate

Executing SSH with the Linux/Unix at command

I place this ssh call in the following a shell script on our Linux box named "tstz" and then call it with the linux "at" command in order to schedule it for later execution.
tstz script:
#! /bin/ksh
/usr/bin/ssh -tt <remote windows server> pmcmds ${fl} ${wf} < /dev/null >/tmp/test1.log 2>&1
at command syntax:
at -f tstz now + 1 minute
The ssh call executes remote command as expected, but the ssh connection closes immediately before the remote command has completed. I need the connection to stay open until the remote command has completed and then return control to the tstz script with an exit status.
This is the error I get in the /tmp/test1.log:
tcgetattr: Inappropriate ioctl for device
^[[2JConnection to dc01nj2dwifdv02.nj.core.him closed.^M
NOTE: When using the "at" command to schedule tstz, if I don't use -tt, the ssh command will not execute the remoted command "pmcmds ${fl} ${wf}". I believe this is because a terminal is required. I can however run tstz from the Linux command prompt in the foreground without the -tt on the ssh command line and it runs as expected.
Any help would be greately appreciated. Thanks!
As I understand you need to specify a command to execute on the REMOTE machine after successfully connecting to the server, not on LOCAL machine.
I use following command
ssh -i "key.pem" ec2-user#ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com -t 'command; bash -l -c "sudo su"'
where you should replace "sudo su" with your own command, I guess with "pmcmds DFD_ETIME wf_TESTa"
So, try to issue, maybe
/usr/bin/ssh -tt <remote windows server> 'command; bash -l -c "pmcmds DFD_ETIME wf_TESTa"'
P.S. I have discovered interesting service on google called "explainshell"
which helped me to understand that "command;" keyword is crucial inside quotes.

Different output of command over ssh

I want to dump cpu usage of a particular process over ssh using top and I want full command line to be shown.
When I ssh to the server and execute command locally, I see following:
remote-server$ top -c -b -n 1 |grep redis-server
5137 redis-user 20 0 83.5g 23g 884 S 13.7 29.3 13388:28 ./bin/redis-server *:11000
But when I execute the same command over ssh, I see following:
local-desktop$ ssh news-cache1 "top -c -b -n 1 |grep redis-server"
5137 redis-user 20 0 83.5g 23g 884 S 13.7 29.4 13388:55 ./bin/redis-server
I don't understand why I don't get complete command line (with host and port arguments *:11000) when I run the command over ssh.
Can anyone tell me what I am doing wrong?
My local desktop is OS X, El Capitan while remote server is centos 6.
Rerun the command with -t option in ssh.
local-desktop$ ssh -t news-cache1 "top -c -b -n 1 |grep redis-server"
ssh client assigns a tty terminal with limited width when you run commands remotely. The width of the terminal assigned was not enough to show the full line that you are interested in. Adding -t forces a pseudo-terminal allocation. From http://man.openbsd.org/ssh
-t Force pseudo-terminal 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.

Is it possible to run multiple command with remote command option in putty?

I want to run multiple commands automatically like sudo bash, ssh server01, ls , cd /tmp etc at server login..
I am using Remote command option under SSH in putty.
I tried multiple commands with delimiter && but not working.
There is a some information lacking in your question.
You say you want to run sudo bash, then ssh server01.
Will sudo prompt for a password in your remote server?
Assuming there is no password in sudo, running bash will open another shell waiting for user input. The command ssh server01 will not be run until that bash shell is exited.
If you want to run 2 commands, try first simpler ones like:
ls -l /tmp ; echo "hi there"
or if you prefer:
ls -l /tmp && echo "hi there"
Does this work?
If what you want is to run ssh after running bash, you can try :
sudo bash -c "ssh server01"
That is probably because the command is expected to be a program name followed by parameters, which will be passed directly to the program. In order to get && and other functionality that is provided by a command line interpreter such as bash, try this:
/bin/bash -c "command1 && command2"
I tried what I suggested in my previous answer.
It is possible to run 2 simple commands in putty separated by a semicolon. As in my example I tried with ls and echo. The remote server runs them and then the session closes.
I also tried to ssh to a remote server that is configured for not asking for a password. In that case, it also works, I get connected to the 2nd server and I can run commands on it. Upon exit, the 2 connections are closed.
So please, let us know what you actually need / want.
You can execute two consecutive commands in PuTTY using a regular shell syntax. E.g. using ; or &&.
But you want to execute ssh server01 in sudo bash shell, right?
These are not two consecutive commands, it's ssh server01 command executed within sudo bash.
So you have to use a sudo command-line syntax to execute the ssh server01, like
sudo bash ssh server01

linux - command line to get version of terminal

I use a terminal which I don't know the name. By which command can I find the name and version of that?
(I use android operating system)
echo $TERM tells you kind of terminal you are using, eg. xterm
Use the ps command with no arguments to get the processes running under the current shell.
Example:
% ps
PID TTY TIME CMD
1917 pts/0 00:00:00 zsh
13659 pts/0 00:00:00 ps

Resources