Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:40:49+00:00 2026-05-30T13:40:49+00:00

How to get the pid in tty1 of the process launched in tty2 ?

  • 0

How to get the pid in tty1 of the process launched in tty2 ?

Context :
Trying to write a bash one-liner to kill a process generating a file when this file exceeds a pre defined max size. (The one-liner is not operating yet as it is as need to embed this into a loop).

During testing, the point is that lsof does not return any PID in the terminal tty1 despite the pid exists in the tty2 where the command is run.

tty1: generating the file and monitoring changes

MAX_SIZE_Ko=10001;file=test_lsof;dd if=/dev/zero of=$file bs=1k count=800;inotifywait $file;SIZE_Ko=$(du -s $file | cut -f1); [[ "$SIZE_Ko" -gt "$MAX_SIZE" ]] && ( PID=$(lsof $file | tail -n1 | awk -F" " '{ print $2 }') ; [[ ! -z $PID ]] && kill -9 $PID || echo "no running PID modifying $file" )

tty2 : increasing the file size

for (( 1; 1; 1));do echo -e "foobar\n" >> test_lsof; echo $(( i++ ))" - pid="$$; done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:40:50+00:00Added an answer on May 30, 2026 at 1:40 pm

    As mentioned in the other answer, the file is opened only for a short time, so the odds of your lsof catching it are low.

    However, you can change that:

    exec 5>test_lsof
    for (( 1; 1; 1)); do
        echo -e "foobar\n" >&5  
        echo $(( i++ ))" - pid="$$
    done
    

    This uses advanced shell redirection – the exec line opens a file descriptor, the >&5 redirects output from the command to that file descriptor.

    If you do that, the shell will be visible to lsof.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

php file.php & How to get PID of the background process in file.php ?
I'm trying to run a java (jnlp) applet from bash and get the PID
I'm trying to get the PID of a process having the class name. The
in bash I need to get the pid of a running process whose I
How does one get their current process ID (pid) from the Linux command line
I currently am starting a Java process, and am trying to get its PID
How can I get the PID of a TERMINAL running a process with given
Here's my C method to get the pid of the Finder process. GetProcessInformation() is
Trying to mount a device and get the pid of mount command. cmd="/bin/mount /dev/sda1
I have a for loop to get the list of PID's and kill each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.