Write a bash script to restart a daemon [closed] - multithreading

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I thought I could just use this related question: How Do I write a bash script to restart a process if it dies. #lhunath had a great answer and told me everything I might do about it was wrong, but I'm restarting a daemon process and if I'm hoping there's something I can do in a single script that works.
my process starts with a kick off script that shows the startup log, but then quits and leaves the process running off the shell:
>sudo ./start
R CMD Rserve --RS-conf /var/FastRWeb/code/rserve.conf --vanilla --no-save
...
Loading required package: FastRWeb
FastRWeb: TRUE
Loading data...
Rserv started in daemon mode.
>
The process is up and running,
ps -ale | grep Rserve
1 S 33 16534 1 0 80 0 - 60022 poll_s ? 00:00:00 Rserve
Is there a simple way to wrap or call the 'start' script from bash and restart when the process dies or is this a case where PID files are actually called for?
Dang - question got closed even after pointing to a very similar question that was not closed on stackoverflow. you guys suck

A very simple way to monitor the program is to use cron: check every minute (or so) if the program still is alive, ./start it otherwise.
As root, invoke crontab -e.
Append a line like this:
* * * * * if ! pidof Rserve 2>&1 >/dev/null; then /full/path/to/start; fi
This method will stay persistent, i.e., it will be executed after a reboot etc. If this is not what you want, move it to a shell script:
#! /bin/bash
# monitor.sh
while true; do
if ! pidof Rserve 2>&1 >/dev/null; then /full/path/to/start; fi
sleep 10
done
This script has to be started manually from the command line, and can be easily stopped with Ctrl-C.

The easiest solution, if you can run the process is NON-daemon mode, is to wrap it in a script.
#!/bin/bash
while (true)
do
xmessage "This is your process. Click OK to kill and respawn"
done
Edit
Many deamons leave a lock file, usually in /var/lock, that contains their PID. This keeps multiple copies of the deamon from running.
Under Linux, it is fairly simple to look throgh /proc and see if that process is still around.
Under other platforms you may need to play games with ps to check for the processes existence.

Related

How to kill a process in bash through terminal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This might be a very basic question but I couldn't find it anywhere on the internet.
Lets assume I have a file named test with this code
echo hello
sleep 10
echo hello
sleep 10
echo hello
sleep 10
How would I go about killing that program through another terminal in my server?
I am assuming the file is test.sh
You can do:
ps -x | grep ./test.sh
This will show the processes:
11164 pts/1 S+ 0:00 /usr/bin/bash ./test.sh
and a second process that will be a grep process, you won't be able to kill the process that has the word grep in it because that process completes right away
now you can kill the process using the PID:
kill 11164
Your script filename is test.
So, in another terminal, you can execute ps aux | grep test.
Then you can get the PID of test, which is located at the second column.
Then, execute kill -9 <PID>.
Ctrl c
By Pressing this, you can kill that program from your terminal.
And you can kill this program from your main terminal where you exicute this in first place.

How to kill a running script from another script? [closed]

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.

Process killed after closing terminal SSH [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I'm trying to clear facebook cache on my server every 2 seconds so i logged in the SSH and i run this command
while true; do sleep 2; curl -F id="http://twd.ma" -F scrape=true -F access_token='token' -F appID=appID https://graph.facebook.com; done &
And every thing worked fine and the cache started to be cleaned every 2 seconds. However, when i close the Terminal SSH the cache stop being cleaned and i think the process is killed, what should i do please?
Your command will stop executing because when you log out, the shell is lost. The '&' means that the script runs in background "as long as the shell is active"
You can do the following:
Write your script into a file, i.e. clearcache.sh and omit the '&'
#!/bin/bash
while true; do
sleep 2
curl -F id="http://twd.ma" -F scrape=true -F access_token='token' -F appID=appID https://graph.facebook.com
done
Write the path to your script into /etc/rc.local
/path/to/clearcache.sh > /dev/null 2&>1 &
The ' >/dev/null 2&>1 means that all output that your script produces will be deleted.
If screen is available to you then you can start a screen session by running screen, run your commands, then press ctrl-a ctrl-d to detach the session.
When you log in later you can issue screen -r to reconnect to the detached session.

How to kill background task from another session? [closed]

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 9 years ago.
Improve this question
I've run a multithreading program in background:
./my_task &
Then I logged out, then logged again. Now jobs command does not show this program, but top does show the threads of this program. So it is still running. How to stop it? I guess I can kill each thread, but there are many of them and I don't know how it will affect my_task program.
I am using Debian Squeeze.
In common case, you can use
ps aux | grep my_task
-or, if you know, that process name starts with "my_task" exactly:
ps aux | grep [m]y_task
(this will exclude grep process itself from result table)
to get desired process id (let it be $pid) and then kill it with kill $pid
edit (thanks to comments below): jobs is part of bash itself, and so information about it is listed in man bash page:
Job control refers to the ability to selectively stop (suspend) the
execution of processes and continue
(resume) their execution at a later point. A user typically employs this facility via an interactive
interface supplied jointly by the operating system kernel's terminal driver and bash.
The shell associates a job with each pipeline. It keeps a table of currently executing jobs, which may
be listed with the jobs command. When bash starts a job asynchronously (in the background), it prints a
line that looks like:
[1] 25647
indicating that this job is job number 1 and that the process ID of the last process in the pipeline
associated with this job is 25647. All of the processes in a single pipeline are members of the same
job. Bash uses the job abstraction as the basis for job control.
but this will not help a case since it will list jobs only for current instance (which, of cause, will change when you're changing your session)
run your proces with log. I have used gnome-calculator for example:
gnome-calculator & echo $! > tmp/11/mylog
and add below to .bashrc or other autostart for kill it:
kill `cat tmp/11/mylog`
You can use pgrep to find the command:
$ pgrep my_task
4384
Then you can use that output to make sure it's the command you want:
$ ps -fp 4384 | cat
I pipe the output to cat because the ps command will chop off the output at the rowsize of the terminal unless it's piped to another command.
You could combine them too:
$ ps -fp $(pgrep my_task) | cat
You can also use pkill if you're brave:
$ pkill my_task
This will kill any processes that match the regular expression my_task that is owned by the user.

How to write a program which will be running automatically, whenever I turn on my computer, in Linux? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am creating a process every time but after "kill -9 -1", I lost the process that I created. I know why I lost it every time..
But is there anyway, so that I can make my program run automatically, every time I turn on my computer??
thanks,,
Most distributions still support SysV Init Scripts.
The easiest way to do it is to take a simple init script from /etc/init.d/ and change it to suit your needs:
sudo cp /etc/init.d/foo /etc/init.d/my_foo
sudo gedit /etc/init.d/my_foo
Then, you'll need to enable it:
sudo /sbin/chkconfig my_foo on
If chkconfig isn't available, you may need to install it. Also, there are LSB aliases like insserv which might be available.
Ubuntu systems now come with Upstart, whose configuration files may be a bit less verbose than with System V init scripts. A simple job configuration for Upstart would look like this, and go into, say, /etc/init/example.conf:
# this is a comment
start on startup
stop on shutdown
exec /path/to/program --some-args maybe-another-arg
Then it'll start and stop, well, on startup and on shutdown, respectively. To manually start and stop it, use the start and stop commands as root:
$ sudo start example
$ sudo stop example
You can find more information about Upstart configuration in its Cookbook. Information is also available in the init man page in section 5 on systems where Upstart is installed. (man 5 init)
Supervisor will let you do that, as well as having some other features like FastCGI support, automatically respawning services if they crash (while being smart about it and not restarting it if it keeps crashing over and over again), and keeping logs of its output.
After it is installed and itself configured to run on startup, you can modify its configuration file to add a section that runs a program. A simple example might be like this:
; this is a comment
[program:example]
command = /path/to/program --some-args maybe-another-arg
That's really all that's necessary for a simple program, but many other configuration options are available; see the documentation.
Once you've added your configuration, you can tell Supervisor to add/remove (and start/stop) any processes you've added or removed from the configuration:
$ sudo supervisorctl update
You can manually start and stop services if you want to, as well:
$ sudo supervisorctl start example
$ sudo supervisorctl stop example
$ sudo supervisorctl restart example
You can also see a nifty status display for all of your processes, e.g.:
$ sudo supervisorctl status
cgi-pass RUNNING pid 4223, uptime 68 days, 23:57:22
And also see what it's recorded of your program's output:
$ sudo supervisorctl tail example # stdout
$ sudo supervisorctl tail example stderr # stderr
$ sudo supervisorctl tail -f example # continuous
Documentation of the available commands is available with supervisorctl help.
Fedora comes with systemd, many other Linux distributions are adopting it (except Ubuntu and for now Debian). The package includes several helper programs you'd might want to look at.

Resources