Linux directory start process

Linux: How to know where a process was started and how it was started?

I was checking a Linux box and found a perl process running and taking a good share of cpu usage. With top, i could only perl in process name. When i pressed c, to view the command-line, it showed /var/spool/mail. Which does not make sense, since this is directory. My questions are: 1) Why did this happen? How this perl process could mask its command-line? 2) What is the most reliable way of finding out where and how a process was started? Thanks!

9 Answers 9

In most cases just running ps is usually sufficient, along with your favorite flags to enable wide output. I lean towards ps -feww , but the other suggestions here will work. Note that if a program was started out of someone’s $PATH , you’re only going to see the executable name, not the full path. For example, try this:

$ lftp & $ ps -feww | grep ftp lars 9600 9504 0 11:30 pts/10 00:00:00 lftp lars 9620 9504 0 11:31 pts/10 00:00:00 grep ftp 

It’s important to note that the information visible in ps can be completely overwritten by the running program. For example, this code:

int main (int argc, char **argv)

If I compile this into a file called «myprogram» and run it:

$ gcc -o myprogram myprogram.c $ ./myprogram & [1] 10201 

And then run ps , I’ll see a different process name:

$ ps -f -p 10201 UID PID PPID C STIME TTY TIME CMD lars 10201 9734 0 11:37 pts/10 00:00:00 foobar 

You can also look directly at /proc//exe , which may be a symlink to the appropriate executable. In the above example, this gives you much more useful information than ps :

$ls -l /proc/9600/exe lrwxrwxrwx. 1 lars lars 0 Feb 8 11:31 /proc/9600/exe -> /usr/bin/lftp 

Источник

Executing chdir before starting systemd service

I have a service that I want to start with systemd . When that service is being started, I want to be able to assign it a current working directory. I know how to do this if I was using init , but I’m having trouble with systemd . Here’s what I’ve been trying to get working.

My Service

#! /usr/bin/python import os print 'Current working directory: %s' % (os.getcwd()) 

My Configuration File

I then created a listdir.service file for systemd and placed it here: /lib/systemd/system/listdir.service :

[Unit] Description=Test of listing CWD. [Service] ExecStartPre=chdir /usr/local ExecStart=/opt/bin/listdir StandardOutput=syslog StandardError=syslog [Install] WantedBy=multi-user.target 

Problem

When I run systemctl start listdir my system log records the root directory («/») as the current working directory. Of course, I expected /usr/local as the current directory, since I thought ExecStartPre would change directories before starting the process. Obviously, I’m imagining that systemd would work something like a shell script (even though I know it isn’t a shell script). Can someone give me an idea of what I should be doing? Is it even possible to set a working directory using systemd ? Thanks! Edit: My system log is reporting an error. (I just noticed.)

Executable path is not absolute, ignoring: chdir /usr/local 

So, chdir is a shell command, and not an executable itself. Okay. But is there still some way for me to change directories using systemd ?

Читайте также:  Удаление папки линукс терминал

Источник

Find out current working directory of a running process?

What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.

7 Answers 7

There are 3 methods that I’m aware of:

pwdx

lsof

/proc

Examples

$ pwdx 12136 12136: /home/saml 
$ lsof -p 12136 | grep cwd nautilus 12136 saml cwd DIR 253,2 32768 10354689 /home/saml 

Or you can poke directly into the /proc :

$ readlink -e /proc/12136/cwd/ /home/saml 

In Ubuntu Server 18.04 the above commands require root privileges. At least in OpenBSD, you can see this answer to my U&L question.

I assume that you have the process ID in pid . Most methods on most systems will require that the shell you’re doing this from is running as the same user as the target process (or root).

On Linux and Solaris and perhaps some other System V unices:

On Linux (except embedded systems where readlink is not available) but not Solaris:

On just about any unix variant, you can use lsof . Beware that if there is a newline, it will be printed as \n (indistinguishable from backslash followed by n ). If you feel lucky, you can use the second form, which silently chokes on all whitespace in the directory name.

lsof -a -Fn -p $pid -d cwd | sed -e '1d' -e '2s/^n/' lsof -p $pid | awk '$4=="cwd" ' 

Bonus: if you need to cause a process to change its current directory, you can do it with a debugger. This is useful for example to move a long-running program that doesn’t care about its current directory out of a directory that you want to remove. Not all programs appreciate having their current directory changed under their feet — for example a shell is likely to crash.

#!/bin/sh # Use gdb to change the working directory of a process from outside. # This could be generalized to a lot of other things. if [ $# -ne 2 ]; then echo 1>&2 "Usage: $0 PID DIR" exit 120 fi case "$1" in *[!0-9]*) echo 1>&2 "Invalid pid \`$1'"; exit 3;; esac case "$2" in *[\\\"]*) echo 1>&2 "Unsupported character in directory name, sorry." exit 3;; esac gdb -n -pid "$1" -batch -x /dev/stdin  

Источник

How To Start A Linux Process?

Starting a Linux process is relatively straightforward, and there are several ways to do it. This tutorial will cover three common methods to start a process: using the command line, creating and running a script, and using a desktop environment application launcher.

1. Start a Process from the Command Line

To start a process from the command line, simply type the command or binary name and press Enter. This will execute the command or start the application.

For example, to start the nano text editor, type the following in your terminal:

Some applications or commands require arguments or options. You can provide them by adding them after the command name.

For example, to open a file called file.txt with the nano text editor:

2. Start a Process by Creating and Running a Script

You can also create a script to start a process. We'll demonstrate this using a Bash script.

#!/bin/bash # This script starts the 'nano' text editor nano

The nano text editor will open as the script runs.

3. Start a Process Using a Desktop Environment Application Launcher

If you're using a desktop environment like GNOME, KDE, or XFCE, you can start a process using the application launcher or menu.

  • Open the application launcher or menu (usually accessible by clicking an icon in the panel or pressing a key combination like the "Super" key, also known as the "Windows" key).
  • Browse or search for the application you want to start.
  • Click on the application's icon to start the process.

By following these methods, you can start a Linux process from the command line, a script, or a desktop environment application launcher.

Professional provider of PDF & Microsoft Word and Excel document editing and modifying solutions, available for ASP.NET AJAX, Silverlight, Windows Forms as well as WPF. We are dedicated to provide powerful & profession PDF/Word/Excel controls.

Источник

How do I run a program with a different working directory from current, from Linux shell?

Using a Linux shell, how do I start a program with a different working directory from the current working directory? For example, I have a binary file helloworld that creates the file hello-world.txt in the current directory.

This file is inside of directory /a . Currently, I am in the directory /b . I want to start my program running ../a/helloworld and get the hello-world.txt somewhere in a third directory /c .

I discovered the hard way that su resets the working directory to the home directory of user you specify before running any -c commands. This was very helpful to me.

It's been 12 years since I've posted this question and today I looked it up myself, cause I could not recall how to do that.

It's one thing to come back to a SO post with a:visited link and an upvote already on it. It's another thing when you're the asker, or even the answerer. There should be a badge for that. "Forgetful"

11 Answers 11

Call the program like this:

The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c , then executes helloworld from /a . After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.

Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c , make the execution of helloworld conditional:

Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld via exec:

[Thanks to Josh and Juliano for giving tips on improving this answer!]

the entire () construct will have the exitcode of the last command inside. You can check that either directly through chaining or by inspecting $? .

Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:

This way is more similar to how you usually run commands on the shell. To see the practical difference, you have to run ps ef from another shell with each solution.

Just say it here — exec replaces the shell process with the given command, without creating a new process.

An option which doesn't require a subshell and is built in to bash

$ pwd /home/abhijit $ pushd /tmp # directory changed $ pwd /tmp $ popd $ pwd /home/abhijit 

A similar suggestion has been done below by Sahil. It does not work if the command fails. Consider pushd SOME_PATH && run_stuff && popd -- if run_stuff fails, than popd is not going to be executed.

Late reply, that depends on the settings of the bash file. Bash can continue executing commands even after a failed command (unlike using &&), but it can be set to not do that using set -e in the file and then it would fail to popd .

Still, I think pushd "$" && run_stuff; popd is better than the current answer, since the pushd/popd semantics were specifically designed for this situation of going into some directory and then coming back to the original one.

I think you'd need to write a shell script to which you would pass the parameter that would execute the series of commands as you can't pass a parameter in the middle of an alias. If you need help writing that, you should ask a separate question and reference this one. Once you have the shell script, you could write an alias to call your new script.

Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:

I always think UNIX tools should be written as filters, read input from stdin and write output to stdout. If possible you could change your helloworld binary to write the contents of the text file to stdout rather than a specific file. That way you can use the shell to write your file anywhere.

$ cd ~/b $ ~/a/helloworld > ~/c/helloworld.txt 

cd SOME_PATH && run_some_command && cd -

the last 'cd' command will take you back to the last pwd directory. This should work on all *nix systems.

One way to do that is to create a wrapper shell script.

The shell script would change the current directory to /c, then run /a/helloworld. Once the shell script exits, the current directory reverts back to /b.

Here's a bash shell script example:

If you always want it to go to /C, use an absolute path when you write the file.

The following worked for my simple need to set up an alias to which I can append an argument, without having to put the instructions in a function; worked in Bash and ZSH:

$ CWD=/your/target/dir command args 

My use case: I have a shell script named run-service that resolves paths to other files based on its own location. If I call it with cd ~/Code/company/scripts && run-service dev/some-service , it will expect to find a config file in ~/Code/company/services/dev/some-service . So instead of always having to cd into the directory and calling the script, I just did this:

# Alias definition alias run-service="CWD=/your/target/dir run-service" # Calling the alias $ run-service foo 

It's probably too simplistic to be of general use, but it works for my basic use-case.

Источник

Оцените статью
Adblock
detector