Skip to main content
added explanation
Source Link
Seamus
  • 3.9k
  • 2
  • 21
  • 49

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question above.

In thinking about this problem, I developed another approach that also seems to work. It's shown below as a bash function; it utilizes the ability of pstree to provide the entire process tree for the PID of the current shell ($$). Also note that usage of the regex [c]ron, prevents grep from including its own process in the output. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question above.

In thinking about this problem, I developed another approach that also seems to work. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question above.

In thinking about this problem, I developed another approach that also seems to work. It's shown below as a bash function; it utilizes the ability of pstree to provide the entire process tree for the PID of the current shell ($$). Also note that usage of the regex [c]ron, prevents grep from including its own process in the output. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}
deleted 5 characters in body
Source Link
Seamus
  • 3.9k
  • 2
  • 21
  • 49

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question here above.

In thinking about this problem, I developed another approach that also seems to work. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question here above.

In thinking about this problem, I developed another approach that also seems to work. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question above.

In thinking about this problem, I developed another approach that also seems to work. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}
Source Link
Seamus
  • 3.9k
  • 2
  • 21
  • 49

The genesis of the Question here concerns how to make a reliable determination as to whether a script was started from a cron job - or from the command line (or some other method; e.g. systemd). In fact, it was my attempt to implement a suggestion in another answer here on SE that led to the question here above.

In thinking about this problem, I developed another approach that also seems to work. I'll share it here as it may be of interest to some:

cron_ancestry() {
    if [[ -n $(pstree -aps $(echo $$) | grep -m 1 "[c]ron,") ]]; then
        echo "cron is your daddy"
    else
        echo "spawned by unknown"
    fi
}