linux - process already running error - linux

I am trying to start a process and although ps -ef|grep myprocessname does not show it running, when I invoke the script to start it it says process already running, exiting.
I have searched internet for about one hour and I can not find any answers. Can anyone help? Thank you.
#TILO: There is no file under /var/run that has a name even close to my process. Any other suggestions?
#VKRAM: This is a third party software. Any suggestions?

check under /var/run if there is a .pid file for the process you're trying to start.
e.g. /var/run/mysqld/mysqld.pid would be such a file.
That file contains the PID of the process...
run a
ps -edaf | grep PID # with the pid you find in the file
if the process is not found, you can delete the pid-file -- then try starting your process again

Try using strace on the program in question:
strace yourprogram
Shortly before it terminates, you should see the system calls it used to determine that another instance was running, and can from there reverse engineer the method it is using.

you said you can't find the PID-file...
If you can't find the PID file (maybe because some of the directories under /var/run are deeply nested),
try this to see a list of all PID-files in there:
find /var/run -type f -name '*.pid'
find /var -type f -name '*.pid'
maybe you'll see a filename that looks similar to the process name you're trying to start.
Or you can also put a grep at the end of that line and try to grep for the process name in the list.
Some programs put use also lock files -- these can be usually found under /var/lock/ or /var/lock/subsystem
If that doesn't help, try to look at the start-script that you're using , e.g. under /etc/init.d/
Look at it in detail and look for something like LOCK_FILE or PID_FILE

Related

Check which processes are running from a given location in bash

I have the following directories structure:
./folder1
-- file1.py
-- file2.py
./folder2
-- file3.py
-- file4.py
etc...
useless_file_a.py
useless_file_b.py
I am trying to write a bash script to know which of file[i].py processes are running. By running, I mean to see the process displayed as a result of the ps -elf command.
My idea is to:
Loop through those folder[i] and list the .py files available. For this step I can use:
find . -type f -name "*.py" -mindepth 2
Check running processes that matches the names found in 1.
I know about the ps -elf command and I would like to link it to step 1.
I am struggling to come up with something for this last step.
The whole point of this script is to alert me if one of those .py scripts are not running. As there can be many scripts and many folders, I want to automatize this process.
NB: The useless_file_*.py are in the pwd and are not relevant for me.
It's not possible, generally, to find out "if a script is running". This information is not stored anywhere.
You can check if the string that is the filename is in the command line arguments of processes:
for f in */*.py; do
pgrep -f "$(basename "$f")"
done

Find process ID of a background process

I am execute a infinite while loop from command line that would execute a rest call with the use of CURL . I'm redirecting the rest o/p to a file . I have appended an "&" at the end of the command so that it goes into background.
Unfortunately, the terminal was exited .
But I see the process is still writing the o/p to the file continuously .
However I don't see any background jobs running with the command job.
Even I used ps/lsof/fuser to find the process ID so that I can manually kill it.
But none of the commands returned me the process ID.
I even tried changing the file to read only mode, but still I see the file continuously growing.
At last I found chattr command that would restrict the file from being written.
But in this case where would I find the process ID that is responsible .
touch /var/log/test_mon_vm.log;while true; echo "start....." >>/var/log/test_mon_vm.log;echo $(date) >> /var/log/test_mon_vm.log; do /usr/bin/curl -I --user rhqadmin:rhqadmin http://localhost:7080/rest/plugins?name=Samba 2>/dev/null | head -n 1 >> /var/log/test_mon_vm.log; echo "end....." >> /var/log/test_mon_vm.log;echo >>/var/log/test_mon_vm.log;echo >>/var/log/test_mon_vm.log;sleep 1;done &
You can find the ID with
ps -fea|grep [your USER]
then search the script list, there's gonna be, if you use an script replace your user with the script name
You can use fg
Background Processes in linux
once the background process is in the foreground you can kill it using ctrl+c
I hope this helps! Have a nice day!
You can try
ps aux | grep "command-you-ran"
e.g.
ps aux | grep "curl"

Is a script called somewhere

On one of my linux servers I have a script that performs some controls.
Is there a way of finding out where this script is called? This can be in
another script, cobol program, crontab, ...
Opening every one of them will take a very long time.
If you can modify the script, put in a ps line to get the parent pid, ps again and grep for the parent pid to get the command, then log to file.
Come back in a week or so and you should have the command that is triggering your script. In case it's something nested, you may want to recurse or similar.
To do this without modifying the script, you'll need a watcher script/program that checks for access to the script file or calls ps every so often. However, if you have that kind of access, just modifying the script is probably easier.
Edit: Apparently the commands to get the parent pid and command for it, without repeatedly calling ps, look something like:
ps -p $$ -o ppid=
cat /proc/<pid>/cmdline
(from jweyrich's answer here)
Grep for it:
grep -lr yourscript /etc /opt/anotherlikleydir
failing that, search the whole system : grep -lr yourscript /
Edit:
failing that, search in binaries too: grep -lar yourscript /
failing that, the script is either executed by a logged in user or a scripted remote login... if that's the case, try peachykeen's approach and edit the script... and why not dump a ps axf to a log too.

How to restart background php process? (how to get pid)

I'm a PHP developer, and know very little about shell scripting... So I appreciate any help here.
I have four php scripts that I need running in the background on my server. I can launch them just fine - they work just fine - and I can kill them by looking up their PID.
The problem is I need my script to, from time to time, kill the processes and restart them, as they maintain long standing HTTP requests that sometimes are ended by the other side.
But I don't know how to write a command that'll find these processes and kill them without looking up the PID manually.
We'll start with one launch command :
/usr/local/php5/bin/php -f /home/path/to/php_script.php > /dev/null &
Is there a way to "assign" a PID so it's always the same? or give the process a name? and how would I go about writing that new command?
Thank you!
Nope, you can't "assign" the process PID; instead, you should do as "real" daemons do: make your script save its own PID in some file, and then read it from that file when you need to kill.
Alternative would be to use something like supervisor, that handles all that for you in a quite nice way.
Update - supervisor configuration
Since I mentioned supervisor, I'm also posting here a short supervisor configuration file that should do the job.
[program:yourscriptname]
command=/usr/local/php5/bin/php -f /home/path/to/php_script.php
Have a look here for more configuration options.
Then you can use it like this:
# supervisorctl status
to show the process(es) status.
# supervisorctl start yourscriptname
to start your script
# supervisorctl stop yourscriptname
to stop your script
Update - real world supervisor configuration example
First of all, make sure you have this in your /etc/supervisor/supervisord.conf.
[include]
files = /etc/supervisor/conf.d/*.conf
if not, just add those two lines and
mkdir /etc/supervisor/conf.d/
Then, create a configurtion file for each process you want to launch:
/etc/supervisor/conf.d/script1.conf
[program:script1]
command=/usr/local/php5/bin/php -f /home/path/to/php_script.php
stdout_logfile=/var/log/script1.log
stderr_logfile=/var/log/script1-error.log
/etc/supervisor/conf.d/script2.conf
[program:script2]
command=/usr/local/php5/bin/php -f /home/path/to/php_script2.php
stdout_logfile=/var/log/script2.log
stderr_logfile=/var/log/script2-error.log
...etc, etc.. for all your scripts.
(note that you don't need the trailing & as supervisor will handle all the daemonization thing for you; in fact you shouldn't execute programs that are self-daemonizing inside supervisor).
Then you can start 'em all with:
supervisorctl start all
or just one with something like:
supervisorctl start script1
Starting supervisor from php
Of course, you can start/stop the supervisor-controlled processes using the two commands above, even from inside a script.
Remember however that you'll need root privileges, and it's quite risky to allow eg. a web page to execute commands as root on the server..
If that's the case, I recommend you have a look at the instructions on how to run supervisor as a normal user (I never did that, but you should be able to run it as the www-data user too..).
The canonical way to solve this is to have the process write its PID into a file in a known location, and then any utility scripts can look up the file, read the PID, and manipulate that process. Add a command line argument to the script that gives the name of the PID file to write to.
A work around to this would be to use ps aux, this will show all of the processes with the command that called them. This presumes of course that the 4 scripts are different files, or can be uniquely identified by the command that called them. Pipe that through a grep and you're all set ps aux | grep runningscript.php
OK! so this has been a headache and a half for my who knows NOTHING about shell/bash whatever scripting...
#redShadow 's response would had been perfect, except my hosting provider will not give me access to the /etc/supervisor/ directory. as he said, you must be root - and even using sudo was an admin wouldn't let me make any chances there...
Here's what I came up with:
kill -9 `ps -ef | grep php | grep -v grep | awk '{print $2}'`
because the only types of commands I was executing showed up in the top command as php this command loops thru running processes, finds the php commands and their corresponding PIDs and KILLS them! woot!!
What I do is have my script check for a file that I name "run.txt". If it does not
exist, they exit. Then just br renaming that (empty) file, I can stop all my scripts.

Find file launching a process

I think my server has been compromised and it has many perl processes running. However, I don't know what file they are being launched from so I can delete it. How can I find this information?
If your system has been hacked, you cannot trust any of the software, not even the kernel. Format the disk and re-install everything. There is just no way to be sure you've cleaned out the infection, because you can't trust the very tools you would use to clean things. You can't copy new tools onto the box, because you can't trust the SSH daemon or the /bin/cp command. Anything -- ls, vi, ps, cat, dd, etc. -- could have been replaced with a trojan that works to hide the infected files.
You could check the symbolic link /proc/pid/cwd, also check the ppid from ps(1).
The first thing I would do is look at the parent process id (PPID). That said, if the PPID is 1, that doesn't tell you anything.
Auditing the filesystem could help see here
pstree could also help
If you run the command "ps -ef" you should get a list of all processes running on your machine. Each process will have a process id number (PID), and also a parent PID. Find the offending process(es) and check their parent PIDs. Then find the process with a matching PID, and it should be your culprit.
Try ls -l /proc/<pid>/exe, or ls -l /proc/<pid>/fd. I don't remember if perl keeps the script file open after the program starts, but if it does, it will be one of the process's file descriptors.
But if your system is pwned, don't expect anything to make sense.

Resources