Skip to main content
14 events
when toggle format what by license comment
Nov 11 at 11:50 history edited mgutt CC BY-SA 4.0
added 3 characters in body
Nov 11 at 11:15 history edited mgutt CC BY-SA 4.0
added 1 character in body
Aug 1 at 12:17 history edited mgutt CC BY-SA 4.0
added 521 characters in body
Aug 1 at 11:22 history edited mgutt CC BY-SA 4.0
added 53 characters in body
Aug 1 at 11:22 comment added mgutt In the meantime I experienced the same issue as Kusalananda on some machines. Because of that I rewrote the answer, which now relies on pstree (which is a similar answer as @cas posted, which relies on ps commands to obtain the parent process).
Jul 17 at 10:08 history edited mgutt CC BY-SA 4.0
deleted 282 characters in body
Feb 26, 2022 at 10:22 comment added Kusalananda Yes, as the answer that you link to mentions, it is safer to give the script an indication that it is running from cron (which is the specific situation that we want to test for) than relying on what the environment usually looks like. Personally, I would run the script with FROM_CRON=true ./myscript in the cron schedule, and then test for FROM_CRON in the script (if [ "${FROM_CRON-false}" = true ]; then ...; fi), rather than abusing the command line options. It's unclear why this has not been suggested so far. (EDIT: Oh, yes, it's in the accepted answer).
Feb 26, 2022 at 10:11 comment added mgutt Yes, but this does not cover situations when you execute a script by passing the SSH command as a parameter as mentioned in this answer.
Feb 26, 2022 at 8:54 comment added Kusalananda You could test for PS1, the interactive prompt. This is essentially what another answer already suggests, though.
Feb 26, 2022 at 8:49 comment added mgutt @they Thank you for testing. Does SSH_CONNECTION exist in Unix if you connect to the Terminal through SSH? That's an additional variable which could be used in Linux, but it does not exist if the Terminal on the machine itself is used of course.
Feb 26, 2022 at 8:46 history edited mgutt CC BY-SA 4.0
added 4 characters in body
Feb 24, 2022 at 12:02 history edited mgutt CC BY-SA 4.0
added 26 characters in body
Feb 24, 2022 at 10:54 comment added Kusalananda I can't see those environment variables on any Unix I have interactive access to. But what you are basically saying is the inverse of the suggestion at the end of the accepted answer: Test for the absense of some variable that is usually declared in an interactive shell.
Feb 24, 2022 at 10:31 history answered mgutt CC BY-SA 4.0