Guess from where a process is being called [Raspbian] - process

I have a java application running on boot in my Raspberry Pi and it's working fine. But I've noticed that after booting there are 2 processes of the same application, as if it was being called twice.
One is called from /etc/rc.local and the other I don't know from where, but I'd like to know it. Is there any way to achieve this?
Thanks a lot.

This gets the parent PID of a process (the calling process)
cat /proc/1111/status | grep PPid
replace the '1111' with the PID of your process
See https://superuser.com/questions/150117/how-to-get-parent-pid-of-a-given-process-in-gnu-linux-from-command-line

If I do ps -ef I can list all the processes running on my Pi. Second column is PID and third is PPID which is enough for me.

Related

How to kill an unkillable task?

I am using Scilab to access a software called LTSpice (XVIIx64.exe) and whenever I use wmic/taskkill/powershell.exe "Get-Process XVIIx64 | Stop-Process". or whatever killing commands, it doesn't actually kill the process (it does close the program, but it still is there in the memory as shown in the picture below). Once I repeat the code in scilab another instance of LTSPICe (XVIIx64.exe) is created in memory and with time there are so many of them that scilab shuts down as it cannot allocate more memory :(. How can I actually kill this process? It's also strange that in the error message it is mentioned PID XXXX "child process of PID 18208" could not be terminated, however PID 18208 does not show up in tasklist and only PID XXXX shows up (View the attachment).
Did you already checked if it is any malware or something? Do that.
If you've done it and got nothing, then get a backup and format your pc. I think that's the best option.

Beaglebone black ( Debian Image 2014-05-14) goes to sleep after 10 minutes

As the title say it the board goes to sleep after 10 minutes. All I want is to SSH into the board (no keyboard/mouse or monitor attached). After googling for a good period of time all I found are some settings for X (Gui) I have also try the following command:
setterm -blank 0 -powersave off -append
It gives me the following error
setterm: cannot (un)set powersave mode: Inappropriate ioctl for device
How can I fiddle or completely disable this power management. I am pretty sure is not a scheduled task or a process but rather the kernel itself and a setting I couldn't found.
Thanks in advance!
I'm not entirely sure, but if your /sys/power/state file has a state, i.e. some string is returned, try echo -n /sys/power/state to make the file blank.
In my case I left the instalation SD inserted into the board. It wasnt power management but rather the disto linux would install over and over again - therefore the approx 10 minutes interval.
I am pretty sure the answer with the PM settings file is correct however it wasn't my case.
Regards,
DAN

Starting multiple upstart instances automatically

We use PHP gearman workers to run various tasks in parallel. Everything works just fine, and I have silly little shell script to spin them up when I want them. Being a programmer (and therefore lazy), I wanted to see if I could spin these up via an upstart script.
I figured out how to use the instance stanza, so I could start them with an instance number:
description "Async insert workers"
author "Mike Grunder"
env SCRIPT_PATH="/path/to/my/script"
instance $N
script
php $SCRIPT_PATH/worker.php
end script
And this works great, to start them like so:
sudo start async-worker N=1
sudo start async-worker N=2
The way I want to use these workers is to spin up some number of them (maybe one per core, etc), and I would like to do this on startup. To be clear, I don't need the upstart script to detect the number of cores. I'm happy to just say "do 8 instances", but that's why I want multiple running. Is there a way for me to use the "start on" clause in an upstart script to do this automatically?
For example, start instance 1, 2, 3, 4? Then have them exit on shutdown properly?
I suppose I could hook this into an init.d script, but I was wondering if upstart can handle something like this, or if anyone has figured out this issue.
Cheers guys!
What you need is a bootstrap task that runs on startup and iterates over all your worker jobs, starting each one.
#/etc/init/async-workers-all.conf
start on runlevel [2345]
task
env NUM_WORKERS=8
script
for i in `seq 1 $NUM_WORKERS`
do
start async-worker N=$i
done
end script
The key is to make this a task, which tells upstart to let the task run to completion before emitting any events for it. See http://upstart.ubuntu.com/cookbook/#task and http://upstart.ubuntu.com/cookbook/#instance

Observe a Process of Unknown PID (no UI)

I've found this question, but even when is close to what I need, is useless
Basically, I have an app that needs to do something when another process (of known name) is launched and/or terminated, but i don't have the PID, so i can't set a kqueue to look for it.
I could do a while for "ps aux | grep processtolook | grep -v grep" command, but that's my last resort.
Any ideas?
Look at this answer: Determine Process Info Programmatically in Darwin/OSX. The libproc.h header file has proc_listpids which will get you all the pids. You can then get the pid information in a loop and using proc_pidinfo and check the name. Looking at the top source as suggested there might also be worthwhile. (The current verson is here http://www.opensource.apple.com/source/top/top-67/.)
Unfortunately, this is an undocumented interface and subject to change at any time. Also, it isn't the quickest thing, since you have to loop over all the processes in the system. In general, this isn't a great way to do this.
A better way might be to write a new processtolook that simply invoked the old one which you can move or rename. It could then notify you of the process start and stop. Is that a possibility?
If the target process/program name which you want the PID is "processtolook" then you can use pidof command to get the PID of that running program.
pidof processtolook
have a look at the pidof manual. This is a part of sysvinit-tools package.
EDIT1:
Have a look at this code i found: http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html
This might help you get you the outline.
On BSD you can use kvm_openfiles and kvm_getprocs to implement what you want. I don't know if it's available on OSX. If you have a pgrep program, look how it's implemented (src/bin/pkill.c).

How can I put a process to background after its execution with broken CTRL+Z?

The question is special because some keys, such as CTRL+Z, stopped working.
I tried to put the process to background by typing in the order:
find /
CTRL+Z
bg
However, I can still see the stdout. The only difference to only doing the first step is that the command CTRL+Z does not work anymore. It is rather nasty when I have unsaved jobs and my harddrive is over 100GB. So
how can I put the process to background?
[Details]
I am using the fourth version of Bash on Mac.
[Crux Reply by Nicholas Riley]
The problem is really that I do not understand the "ramifications" of running process background. I cannot understand why the commnands, such as CTRL+Z, do not work to background processes. I was still able to kill the process in another shell with the command:
ps -ej | awk '! /grep/ && /find/ {print $2}' | xargs kill -9
^Z isn't working because the frontmost job is now the shell, and shells don't usually respond to SIGTSTP. (If you do really want to suspend a non-login shell, suspend usually works.)
The problem seems to be you misunderstand the ramifications of a job being in the background. Redirecting the job's standard output is unrelated.
In fact, it's unclear what you want to do. If you just want to stop find from running, then fg it and use ^C or ^\ (which by default send SIGINT and SIGQUIT respectively).
If you want to keep find running but suppress its further output, then the easiest solution I can think of is to use bg ; disown ; exit. That will stop the shell from killing its child process (find) and exit the shell; assuming it's at the top level of the Terminal window, you'll see a bit more output and find will keep running (but you'll have no way to interact with it).
I use disown.
find / & disown
exit # close the terminal and the command still runs
You can use disown after you ^Z as well:
find /
^Z
bg
disown
exit
disown is a bash builtin, I believe. Not sure about alternatives for other shells.
For further information, see my equivalent answer on Server Fault.
You can set the operation to a different key with stty on any UNIX-like system.
$ stty susp Q
will make Q your suspend key in place of CTRL-Z.
Backgrounding a process will not disconnect the output from the terminal device, that's why you're still seeing output and the output may well contain control characters which can stuff up your TTY settings (cating binary files is a good way to do that).
If you want the job to run in the background, do it right:
find / >/tmp/out 2>&1 &
then examine the /tmp/out file when it's finished.
I usually do this kind of thing with nohup. Like this:
nohup find / > /tmp/myresults.txt &
nohup makes sure that the process doesn't stop, even if the console goes away (like, you close the window or lose your SSH or whatever). The ">" sends output to a file rather than to the console, and "&" puts the job in the background.
I'll assume you're using a common variant of Linux or UNIX and possibly the bash shell. In which case:
CTRL-Z sends the SIGTSTP signal to the foreground process. You should be able to do the same thing with the kill command by specifying kill -s SIGTSTP [pid].
I know that in Ubuntu 9.04, you can start the process with a & after it to run it in the background. For example, "sudo gedit /boot/grub/menu.lst &" will start a text editor editing the GRUB config file in the background.
kill -STOP your_pid_here
from another console window?
http://tombuntu.com/index.php/2007/11/23/how-to-pause-a-linux-process/#comment-132413