How can I get process command line from PowerShell Core on Ubuntu? - powershell

I'm on a Ubuntu 18.04 server. I know the full command line information can be grabbed by ps auxww. For example, by running ps auxww, I know the command /usr/local/bin/my-program -parameter :8888 is running. How can I get the same info from PowerShell? I searched around and all the info is about how to get the command line info on Windows.

On Ubuntu 18.04 PowerShell,
did you try ps -a -F. It should give the details you are looking for.
if need specific details about the running/all processes try with more options with
ps --help all or ps --help output commands
PS> ps --help output
Usage:
ps [options]
Basic options:
-A, -e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N, --deselect negate selection
r only running processes
T all processes on this terminal
x processes without controlling ttys
Output formats:
-F extra full
-f full-format, including command lines
f, --forest ascii art process tree
-H show process hierarchy
-j jobs format
j BSD job control format
-l long format
l BSD long format
-M, Z add security data (for SELinux)
-O <format> preloaded with default columns
O <format> as -O, with BSD personality
-o, o, --format <format>
user-defined format
s signal format
u user-oriented format
v virtual memory format
X register format
-y do not show flags, show rss vs. addr (used with -l)
--context display security context (for SELinux)
--headers repeat header lines, one per page
--no-headers do not print header at all
--cols, --columns, --width <num>
set screen width
--rows, --lines <num>
set screen height
--help <simple|list|output|threads|misc|all>
display help and exit
For more details see ps(1).

Related

Can you get full command line from process ID (including command line arguments, etc)?

This question is in addition to the question asked here: https://unix.stackexchange.com/questions/163145/how-to-get-whole-command-line-from-a-process. On my system, the following command results in a PID (as expected):
CUDA_VISIBLE_DEVICES=4,5 python3 main.py 1> out.txt 2> err.txt &
Now, the methods in the stack exchange link above provide many solutions. However, when trying these solutions, I only receive the following information:
python3 main.py
Is there a way to return the entire command line "CUDA_VISIBLE_DEVICES=4,5 python3 main.py 1> out.txt 2> err.txt &", not just the portion "python3 main.py"?
No.
Assuming you're on a Linux system, you can find the individual bits, but you can't put it together.
Assume also that the process's PID is in $pid
The CUDA_VISIBLE_DEVICES=4,5 variable gets added to the environment of the python command. You can find it in /proc/$pid/environ but you can't tell which of those variables were specified on the command line: the user could have written
export CUDA_VISIBLE_DEVICES=4,5
python3 main.py 1> out.txt 2> err.txt &
The file redirections are available in /proc/$pid/fd:
/proc/$pid/fd/1 is a symbolic link to out.txt
/proc/$pid/fd/2 is a symbolic link to err.txt
I don't know how to tell if a process is running in the background.
Since you're just interested in the environment: with bash
declare -A environ
while IFS='=' read -r -d '' var value; do
environ["$var"]="$value"
done < /proc/$pid/environ
echo "process has CUDA_VISIBLE_DEVICE value ${environ[CUDA_VISIBLE_DEVICE]}"

How to feed autocomplete data to a fish alias?

I am starting with fish and one of the things I could not find in the extensive documentation was autocomplete feeds.
There is mention of Tab Completions in the tutorial but it addresses existing the existence of the mechanism itself, not its configuration.
I have a bunch of virtual machines I connect to via
machinectl shell <name of machine> /bin/bash
I could make alises for all my machines via
function cm
machinectl $argv shell /bin/bash;
end
but this requires to remember and type the machine name.
How could I use the output of machinectl list | tail -n +2 | head -n -2 | cut -f1 -d' ' as a feed/hint to my cm command so that it shows them when using Tab?
EDIT: I somehow missed this right at the top of the documentation: Tab completion (I found it after reviewing the answers)
This should get you off to a good start:
complete --command cm --no-files \
--arguments '(machinectl list | tail -n +2 | head -n -2 | cut -f1 -d" ")'
Entering that at the command line will activate it for the current session; to make it permanent add the line to a completions file as Kurtis describes (~/.config/fish/completions/cm.fish).
See help complete. You'll find the completions that ship with fish, including completions for ssh, in $__fish_datadir/completions. A completion you write for a private function or command would be placed in ~/.config/fish/completions/$cmd_name.fish

Timestamping netstat runs?

Is there an option that allows me to print a time stamp for the system time of each run of netstat? Done some looking on the man page, but nothing seems to do the trick.
For instance, if I start a run of netstat -vI 10 at 9:30:00, I'd want:
<9:30:00> [INSERT_DATA_HERE]
<9:30:10> [INSERT_DATA_HERE]
etc.
Or is it better if I just write a script to run a 'date' command and pipe the catted output to a text file?
If you are running the current Solaris version (Solaris 11.*), you can use the -T u or -T d option to get a timestamp for each statistic line.
Otherwise, with Solaris 10 and older, there is no builtin option but you can put the start timestamp and the interval in the netstat output filename that way:
netstat -v -I interface 10 > netstat-vI-10s-$(date +%FT%T).out

Command not running fron cron

I have a perl script which runs successfully from the root cron on my redhat server.
However, I have added a command to the perl script to execute an ldapsearch and when running the perl script from the command line it works perfectly, yet running from cron, the ldapseach command does not work.
I've the the full path to the ldapsearch executable and I have tried using both system and exec before the ldapsearch command, but no good.
The line in the perl code does the ldap search for container room, greps the specific line in the results, then parses the data and cuts the 1st two characters of the results. The code is:
$userRoom = `exec /usr/local/bin/ldapsearch -h myldap.domain.com '(cn=$user)' room | /bin/grep -i room | /bin/grep -iv internal | /bin/cut -d'=' -f2 | /bin/cut -c 1-2`;
I'm assuming it's an evironment or permissions thing. I just can't find the right answer.
Any suggestions greatly appreciated.

Batch Script input redirection

Trying to run a program within Windows shell and send more input to the program and keep track of the output. Currently when running the program and sending more input it halts within the program expecting input and then the script sends the input after the program is quit.
cd \Users\user\Desktop\program
#echo "To start program, type "program -a [ipaddress] -r [port number] (EG: program -a xx.xx.xx.xx -r 99)"
program -a xx.xx.xx.xx
show devs
the "show devs" command does not show up until the program is quit. How would I properly call this to get "show devs" to be called within the instance of the program?
Pipe it!
echo "show devs" | program -a xx.xx.xx.xx
Check out the info from this pipes and redirect reference.
Also, enjoy the crazy color scheme from 1995.
As #Frozig said, though remember to escape the pipe if running from a batch file.
echo "show devs" ^| program -a xx.xx.xx.xx
If that doesn't work then the input redirection can be done if you are able to let the batch create a small temp file.
cd \Users\user\Desktop\program
echo show devs>tmp.tmp
#echo "To start program, type "program -a [ipaddress] -r [port number] (EG: program -a xx.xx.xx.xx -r 99)"
program -a xx.xx.xx.xx<tmp.tmp
pause
del tmp.tmp