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


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

By running cron as PID 1 the output is redirected to the file descriptor of PID.

To write the output from cron jobs to PID 1, you can try to redirect the output like this:

# Redirects both stderr and stdout to stdout of PID 1: &>> /proc/1/fd/1
# Redirects stderr and stdout to stdout and stderr of PID 1: 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