0

Sometimes I run jobs, I lose track of which output files I have redirected the output to. How can I get the full command I ran a job with, including the output redirection part?

For example, when I run

nohup python3 -c $'from time import sleep;\nfor i in range(100): print(i); sleep(1)' > .test.txt &

which should print some stuff to .test.txt and run for 100 seconds, and then run ps -fp <PID> or check /proc/<PID>/cmdline, I only get

python3 -c from time import sleep; for i in range(100): print(i); sleep(1)

I've tried some of the solutions on How to get the command line args passed to a running process on unix/linux systems?, but I don't think my command is being truncated.

I notice if the shell I ran the command in is still open when the job finishes, it shows the full path, e.g.

[1]+  Done                    nohup python3 -c 'from time import sleep;
for i in range(100): print(i); sleep(1)' > .test.txt

Is this info only stored in the shell that ran the job?

Edit: @muru I just realised history does give me my command after all; thanks.

  • "... then run ... history" - you should get the full command there. – muru Dec 2 '19 at 2:01
  • If you have the pid of the running process then ls -l /proc/${pid}/fd will show you where stdout (fd 1) and stderr (fd 2) are being written. – doneal24 Dec 2 '19 at 2:40
  • (1) Yes, redirections are not arguments and thus won't show in ps (2) In 'job-control' shells -- the ones that display the completion message you posted, normally at least bash ksh zsh dash tcsh -- the backgrounded jobs not completed yet are listed by the builtin command jobs (3) On Linux and some other Unixes, but not all and you didn't say which you want, redirections for any process to which you have access (not only jobs under your shell) can be examined by looking in /proc/$pid/fd – dave_thompson_085 Dec 2 '19 at 2:52
  • Thanks @dave_thompson_085 @doneal24; I found my output file in /proc/$pid/fd :). If you answer below I'll accept it. – Anonymous Dec 2 '19 at 4:16
1

If you have the pid of the running process then ls -l /proc/${pid}/fd will show you where stdout (fd 1) and stderr (fd 2) are being written.

| improve this answer | |
-2

A single '>' will overwrite the file, try using '>>' which appends to the end of the file.

[admin@srv1 ~]$ for x in $(seq 1 100); do echo $x >> test.txt; done
[admin@srv1 ~]$ head -n 25 test.txt
1
2
...                                                                                                                                                                                                    
| improve this answer | |
  • I don't see how this answers the question of what file was stdout redirected to. – doneal24 Dec 2 '19 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.