grep with regex not working by non-root user - grep

My command is ./test. I want to get the pid of process.
my code:
Distro: Ubuntu 20.04(grep v3.4)
# run command by test user
test#host:~$ ps -ef | grep tes[t]
test ..... ./test
test ..... grep --color=auto test
grep with regex not working.
# run command by root user
root#host:~# ps -ef | grep tes[t]
root ..... ./test
It's Working.
I found that run ps -ef | grep tes[t] by test user(other non-root user also has the same problem), grep with regex not working.
I found that root user is alway working.
ps -ef | grep tes[t]
Distro
It's working?
Ubuntu 20.04 non-root user
❎
Ubuntu 20.04 root user
✅
Ubuntu 22.04 non-root user
✅
Ubuntu 22.04 root user
✅
Why the command is not working on Ubuntu 20.04 with non-root user?
PS: I know ps -ef | grep "tes[t]" (regex string has been quoted) is alwasy working.

Related

How can I restore all of my lost Docker containers?

After a restart of VM I was not able to run any docker command. I follow some question on stack overflow and run the following command ps axf | grep docker | grep -v grep | awk '{print "kill -9 " $1}' | sudo sh
Now There is no image or container.
Result of commands
docker ps and ps -a
docker images list
docker ls
docker ls -a
All the command return empty list. Everything has been clean up.
Is there any way to find our backups or restore the deleted containers?

how to pass output of a command to another in shell script

I am trying to ssh into server, and into a docker container to run the service. however I am not able to store containerId into a variable to pass it to enter the container.
#!/bin/bash
ssh test_server << EOF
ls
sudo docker ps | grep 'tests_service_image' | colrm 13 # This command works
containerId=$(sudo docker ps | grep 'tests_service_image' | colrm 13) # This doesn't
sudo docker exec -i "$containerId" bash # Throws error since containerId is empty
./run.sh
EOF
exit
The problem is that you are doing variable/function expansions on your own side. You need to escape those so that those expansions happen on the server side.
#!/bin/sh
ssh test_server << EOF
containerId=\$(sudo docker ps | grep 'tests_service_image' | colrm 13)
sudo docker exec -i "\$containerId" bash
./run.sh
EOF
exit
Edit:
Pass it directly to docker exec command like so
sudo docker exec -i $(sudo docker ps | grep 'tests_service_image' | colrm 13) bash
Original Answer:
This is written assuming that the script execution is done post sshing into the server. but modified the answer to above based on the specific query
container ID is stored in variable containerId, you are getting the error Error: No such container: because you are passing a different variable $container instead of $containerId to docker exec command.

Docker run exits immediately despite --detach, --interactive, --tty, and providing a command

On a Windows 10 host, Docker version 19.03.13, build 4484c46d9d, I tried running a docker image with all possible combinations of --tty, --interactive, and --detach, but none of them brings me to a bash prompt, always exiting immediately. /bin/bash is present in the image. The Dockerfile is from https://hub.docker.com/r/astj/centos5-vault/dockerfile
I ran:
docker run <switches> astj/centos5-vault /bin/bash
where <switches> had been exercised with the full set of -t, -i, -d combinations, namely:
-d, -i, -t, -it, -id, -td, -dit
In all cases, the container exits immediately.
If I change /bin/bash to ls, I can see a directory listing. But of course, the container exits immediately as expected. To troubleshoot, I experimented with the following commands, with these results:
+-------------------------------------------------------+----------------------------------------------------------------+
| Command | Output |
+-------------------------------------------------------+----------------------------------------------------------------+
| docker run astj/centos5-vault /bin/bash | None. Exits. |
| docker run -i astj/centos5-vault /bin/bash | None. Exits. |
| docker run -it astj/centos5-vault /bin/bash | None. Exits. |
| docker run -t astj/centos5-vault /bin/bash | None. Exits. |
| docker run -td astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -id astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -d astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -dit astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -it astj/centos5-vault ls -la /bin/bash | "-rwxr-xr-x 1 root root 768664 Jul 10 2013 /bin/bash". Exits. |
| docker run -it astj/centos5-vault /bin/bash --version | None. Exits. |
| docker run -it astj/centos5-vault /bin/bash --login | None. Exits. |
| docker run -it astj/centos5-vault /bin/uname -r | "4.19.128-microsoft-standard". Exits. |
| docker run astj/centos5-vault whoami | "root". Exits. |
+-------------------------------------------------------+----------------------------------------------------------------+
I tried to troubleshoot but docker logs <container> doesn't show a single line of log.
Does anyone know why the /bin/bash command still causes the container to exit immediately instead of bringing me to a bash prompt?
I ran into the above issue as well, seems the user above also posted this question separately, and found an answer there:
https://forums.docker.com/t/docker-run-exits-immediately-despite-detach-interactive-tty-and-providing-a-command/99444/5
For anyone finding their way here later, in my configuration mentioned above
this was an issue specific to Windows environments which was resolved
with a change in Windows 10 Build 18995.
Steps to resolve:
Create file : %userprofile%.wslconfig
Add this two lines :
[wsl2]
kernelCommandLine = vsyscall=emulate
Restart wsl
wsl --shutdown
Restart Docker Desktop
I found the answer here: https://github.com/microsoft/WSL/issues/4694#issuecomment-556152512
(Enable vsyscall=emulate in the kernel config to run older base images such as Centos 6)
This worked for me, it seems to only affect certain distributions, for me it was also centos 6

How can I grep a process in BusyBox using ps

I am getting below error in jenkins pipeline when I try to run ps -ef|grep process command:
ps: unrecognized option: p
BusyBox v1.27.2 (2017-12-12 10:41:50 GMT) multi-call binary.
Usage: ps [-o COL1,COL2=HEADER]
Show list of processes
-o COL1,COL2=HEADER Select columns for displayI have Jenkins version 2.135 and
I have BusyBox v1.27.2
Can some one tell me how can I avoid this error for ps -ef|grep process without installing the alpine image
You can use
docker exec -it container_name ps -ef | grep process
or you can do ps -ef | grep process into your container by running docker exec -it container_name /bin/bash

Docker Toolbox - hang on `docker push`

I am using Docker Toolbox on Mac.
docker push is hanging. How do I hard restart the daemon or docker-machine VM to get this unhung in a bad manner. It is taking too long to wait for it.
You can just run:
docker-machine stop <name-of-your-docker-machine>
which is usually:
docker-machine stop default
next option:
docker-machine kill default
Did you check that your docker machine is running:
docker-machine ls
Brute force approach:
kill -9 `ps -Af | grep -v grep | grep VBoxHeadless | awk '{print $2}'`

Resources