Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 months ago.
Improve this question
I start my script bash test.sh and then press Control-Z and get this:
^Z
[1]+ Stopped bash test.sh
All fine. But I want that the output also has the PID.
I know I can do this ps $(jobs -p) afterwards to get the PID. But how it is possible that the output of Control-Z prints directly the PID?
I don't think that's possible. That said, perhaps you can take a step back and clarify why you are hoping to do that?
Because what you can do is directly refer to the particular job with %1 (or %<n> more generally, if you have multiple background jobs) for several built-in commands (fg, bg, kill, ...):
$ sleep 30
^Z
[1]+ Stopped sleep 30
$ kill %1
[1]+ Terminated: 15 sleep 30
More details in man bash or here: https://www.gnu.org/software/bash/manual/bash.html#Job-Control-Basics
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I have a a.sh script that executes a infinite loop with a list of commands in background in a Debian machine and I would like to use another script b.sh to end with a.sh. As far as I know pkill -f a.sh is one way of doing it, but I want to know if there is another way of doing it
man -k kill shows many commands to kill processes. Watch out (1) (general commands) and (8) (admin commands). At my system I get (manually filtered):
killproc (8) - Send signals to processes by full path name
docker-container-kill (1) - Kill one or more running containers
docker-kill (1) - Kill one or more running containers
kill (1) - terminate a process
killall (1) - kill processes by name
killall5 (8) - send a signal to all processes.
pkill (1) - look up or signal processes based on name and other attributes
skill (1) - send a signal or report process status
If you run a.sh from b.sh you can get the process id of a.sh
b.sh
a.sh & # Execute a.sh in the background
APID=$! # $APID is now the pid of a.sh
#do some stuff or wait
kill -SIGTERM $APID # Give the process a chance to shut down
kill -SIGKILL $APID # Certain kill
This way you can kill the particular instance of a.sh if you have multiple concurrent
instances.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I am trying to schedule node server restart on OS reboot (Ubuntu 16.04 LTS). I wrote:
crontab -u username -e
then I added following line:
#reboot /usr/local/bin/forever start -c /usr/bin/node /home/username/node/bin/www
I get the success message after saving or updating this file. There seems to be no effect on server reboot.
I'd wrap that into a bash script in the user's home directory's bin.
/home/username/bin/start_my_node_app.sh
Then in your crontab...
#reboot /home/username/bin/start_my_node_app.sh >/dev/null 2>&1
Though according to this article, #reboot may not work for non-root users.
https://unix.stackexchange.com/questions/109804/crontabs-reboot-only-works-for-root
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I am pretty new to Unix system commands and architecture. I find a few concepts pretty confusing and have been left with some unanswered questions, like: how many shells can be spawned in a single user's login session? does the number of shells have at all anything to do with a user's login?
While exploring system commands, I tried the following:
Opened a terminal window, say Terminal 1. Typed the following commands in order:
sleep 300
^z(ctrl+z)
bg
jobs
ps
Output for jobs:
[1]+ Running sleep 300 &
Output for ps:
PID TTY TIME CMD
3301 pts/1 00:00:00 bash
4494 pts/1 00:00:00 sleep
4497 pts/1 00:00:00 ps
Without wasting any further time (300 seconds were far from being spent), I opened another terminal, lets name it Terminal 2. I entered the following commands in order:
jobs
ps
Output for jobs: -no output, command prompt returned-
Output for ps:
PID TTY TIME CMD
4478 pts/3 00:00:00 bash
4496 pts/3 00:00:00 ps
I am wondering why the backgrounded sleep job (with Process ID 4494), executed in Terminal 1 wasnt listed in Terminal 2, neither with jobs, nor ps.
Can somebody please explain to me what exactly happened here?
The shell in terminal 1 is bash with a PID of 3301. The shell in terminal 2 is bash with a PID of 4478. So yes, each terminal window is running an independent copy of the shell.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I try to kill a process with sudo kill 30602. But after I killed it I use ps aux | grep gmond to check, it appear again with another pid.That's like:
ganglia 30997 0.0 0.1 121812 2128 ? Ssl 16:05 0:00 /usr/sbin/gmond --pid-file=/var/run/ganglia-monitor.pid
Whatever how I kill it, it just appear again with another pid, even with kill -9.
What's the problem? And how to solve this?
You should change the entry in the /etc/inittab file. Probably your gmond service entry is starting with respawn. It will respawn every time you kill the process.
Link: To disable the process you have to edit /etc/inittab and comment out that line. To inform init about this change you have to send a SIGHUP to init:
kill -HUP pid-of-init
The /etc/inittab file was the configuration file used by the original System V init daemon. The Upstart init daemon does not use this file, and instead reads its configuration from files in /etc/init directory.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I would like to kill a particular process (it is a program which name is test.exe) in Linux. To do that I can type:
ps aux | grep test.exe
And then:
kill -9 process_id_of_test_exe
As You can see I need to type commands several times (not immediately). The question is how to do that in one command. Take the process_id of test.exe and give to kill. I think that I need to write a script in such case. Thanks.
Use this command to get PID
pidof test.exe
1044
Then to kill it:
kill 1044
And then join them:
kill $(pidof test.exe)
You can also use your command:
kill -9 $(ps aux | grep [t]est.exe)
The [t] prevents grep from grepping itself.
Try:
killall test.exe
See man killall for more details!
You can combine kill -9 with pgrep like follows:
kill -9 `pgrep test.exe`
For killing all processess with keyword test.exe, try
pkill -9 -f test.exe