Detect if script starts from queue

Dear community,
what I'm try to do is deny users to run a script without parameters from command bash, but the same script should run without parameters only from crontab.

Example runs by crontab:

*/5 * * * * /tmp/script.sh
Here the normal execution starts every 5 minutes

Example #1 runs by user:

# /tmp/script.sh
Output: You cannot start script without parameters

Example #2 runs by user:

# /tmp/script.sh --help
Output: OK I'll give you help
execution starts taking care the parameter

How can I handle that?

Thanks
Lucas

Why not use some --crontab flag to start in crontab ?
env command report your env. Look env output via crontab and not crontab.

--crontab flag like this:

*/5 * * * * /tmp/script.sh --crontab

could be a solution, however "stupid" user :rolleyes: can check the crontab and see the flag, then run the script with --crontab flag causing disaster!

About the env, I don't understand what you mean, could you please report an example?

Lucas

env > /tmp/env.tmp
user=$(logname)
[ "$user" != "mycronuser" ] &&  echo "sorry" && exit 1

Look values. Is there some usable variable. Or run crontab using some special user. Then check in the script, is the user your cron user or not.
Variable is not so good, because user can set 1st the variable and then run the script.

just a hint:

#!/bin/ksh
if [ -t 0 ] ; then
   echo "I am interactive"
else
   echo "I have no controlling terminal - cronned?"
fi;
2 Likes

It works perfect, then, if I use the following code:

CheckInteractive ()
{
if [ -t 0 ] && [ "$#" == 0 ] ; then
  bold=`tput bold`
  normal=`tput sgr0`
  echo ; echo "${bold}WARNING: This script cannot start from command line, use parameters instead (interactive mode)!${normal}"             
  exit
fi;
}

I'll able to run the script only with "parameters". Instead, from crontab, it runs without parameter.

Thanks
Lucas

This can be defeated by redirecting into the script a la < /dev/null .

What do you mean with "redirecting into the script" ? :confused:

what exactly doesn't work?
this works as a stand-alone script - lord.sh

#!/usr/bin/ksh

if [ -t 0 ] && [ "$#" -eq 0 ] ; then
  echo "WARNING: This script cannot start from command line, use parameters instead (interactive mode)!"
  exit
fi;

echo 'param found - good'

This appears to work better:

# Prevent error message spam
exec 2>/dev/null

if ! exec 5</dev/tty
then
        echo "No terminal"
else
        echo "Terminal"
fi

Exactly what I said.

If they run it like ./script < /dev/null then FD0 will not be a terminal, and your check will be bypassed.

To prevent that kind of trickery, go direct to the source. If you can open /dev/tty, you have an interactive terminal. If you can't, you don't.

Unfortunately every failure will cause an error message unless you redirect stderr temporarily.

1 Like

Thanks Corona, but unfortunately I already use STR & STOUT in my script:

exec 1>>/applog/script_$(date +"%Y%m%d").log; exec 2>>/applog/script_$(date +"%Y%m%d").log
....
....
  exec 1>&5
  exec 2>&6
echo "STDERR & STOUT come back to screen"
....
....

I believe your caode overlaps mine...

Then you already know how to preserve and restore file descriptors. The important thing is that you get rid of the error message that moment, afterwards you can put stderr right where it was.

And you don't have to use 5 in my code. Make it 7 since you're already using 5 and 6.

1 Like

Corona, sorry if I resume the thread, but finally I had time to test your code.

CheckInteractive ()
{
if exec 7</dev/tty
then
  bold=`tput bold`
  normal=`tput sgr0`
  echo ; echo "${bold}WARNING: SCRIPT cannot start from command line, use parameters instead (interactive mode)!${normal}"
  exit                 
fi
}

CheckInteractive
Rest of the script

This works perfect when on terminal, but generate error when start from crontab and the rest of the script is not executed. This is the mail content it generated every time script starts from crontab:

TERM environment variable not set.
/tmp/test.sh[345]: /dev/tty: cannot open [No such device or address]

How can I handle that ?

Thanks
Lucas

Add redirect , then cron not send email.

* * * * *   /some/command  >/dev/null 2>&1
1 Like

Thank you, but I notice that the error stop the rest of the script as reported in my post. So the problem is not only the generated mail!
But probably you have replied before I modified my post! :wink:

I did warn you there'd be error message spam. I also told you how to deal with that. Redirect stderr into /dev/null so the messages don't go into your cron mails, but save that FD for later before you do so so you can restore it afterwards.

Your code already does this kind of redirection, you appeared to know how to do so already. I'll show you again though.

CheckInteractive ()
{

exec 10<&2 # Make a copy in FD 10
exec 2>/dev/null

if exec 7</dev/tty
then
  bold=`tput bold`
  normal=`tput sgr0`
  echo ; echo "${bold}WARNING: SCRIPT cannot start from command line, use parameters instead (interactive mode)!${normal}"
  exit                 
fi

exec 2>&10 # Restore stderr
exec 10>&- # Close the copy
}
1 Like