pid snippets

Find out the process id of a process you know how to single out with grep.

Tagged linux, cut, pid, process id  Languages bash

The cut program can be really versatile. Give it the character used to separate columns using the -d switch, and the column(s) you want to print using the -f switch. Here we give ' ' as a parameter to the -d switch because spaces indicate the columns here. A single column is given using -fn and multiple columns using -fn,n,n,n,n syntax.

ps axu |grep FitNesse | grep java |grep -v grep | cut -d' ' -f3

How to get cron to log to STDOUT under Docker and Kubernetes

Tagged cron, dockerfile, pid, stderr, stdout  Languages bash

Dockerfile

FROM python:3.9-slim-buster
...
COMMAND ["cron", "-f"]

In cron scripts, redirect the scripts’ output to the file descriptor of PID 1, which is cron (Dockerfile’s COMMAND):

# Redirects both stderr and stdout to stdout of PID 1:
run.sh &>> /proc/1/fd/1
# Redirects stderr and stdout to stdout and stderr of PID 1:
run.sh 1>> /proc/1/fd/1 2>> /proc/1/fd/2

Each PID (process) has it’s own file descriptors:

/proc/{PID}/fd/0 # STDIN
/proc/{PID}/fd/1 # STDOUT
/proc/{PID}/fd/2 # STDERR