I'd like to use the same scripting for cron and interactive, just wondering if there was a universal way over UNIX/Linux systems to easily make this determination.
I'd like to use the same scripting for cron and interactive, just wondering if there was a universal way over UNIX/Linux systems to easily make this determination.
You can try to return the name of the parent process with something like this: -
For me this returned konsole when I ran this interactively.
or you could script in some logic to handle situations.
Now when executing the script you can pass an option as an attribute like so :-
there is no universal way at all. using interactive/noninteractive shell detection or tty detection is not reliable either, as other cases than cron can have these characteristics. just add a variable in your cron entry. say you need to run test.sh, then use this instead.
This is fairly easy and reliable, if you control the cron setup.
I don't know how portable this is, but on Linux you can run
tty
and check whether the output is a tty.Note that the tty will also not be set if the script is running non-interactively from something other than cron, but since you said you want to distinguish cron from interactive, that shouldn't be an issue.
Other than that, possibly the most robust solution is to add a switch to your script, telling it whether it is running from cron or not, as suggested in a Stack Overflow question.