Detect if script starts from queue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Detect if script starts from queue
# 1  
Old 08-09-2012
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:
Code:
*/5 * * * * /tmp/script.sh
Here the normal execution starts every 5 minutes

Example #1 runs by user:
Code:
# /tmp/script.sh
Output: You cannot start script without parameters

Example #2 runs by user:
Code:
# /tmp/script.sh --help
Output: OK I'll give you help
execution starts taking care the parameter

How can I handle that?

Thanks
Lucas
# 2  
Old 08-09-2012
Why not use some --crontab flag to start in crontab ?
env command report your env. Look env output via crontab and not crontab.
# 3  
Old 08-09-2012
Quote:
Originally Posted by kshji
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:
Code:
*/5 * * * * /tmp/script.sh --crontab

could be a solution, however "stupid" user Smilie 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
# 4  
Old 08-09-2012
Code:
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.
# 5  
Old 08-09-2012
just a hint:
Code:
#!/bin/ksh
if [ -t 0 ] ; then
   echo "I am interactive"
else
   echo "I have no controlling terminal - cronned?"
fi;

These 2 Users Gave Thanks to vgersh99 For This Post:
# 6  
Old 08-09-2012
Quote:
Originally Posted by vgersh99
just a hint:
Code:
#!/bin/ksh
if [ -t 0 ] ; then
   echo "I am interactive"
else
   echo "I have no controlling terminal - cronned?"
fi;

It works perfect, then, if I use the following code:
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
# 7  
Old 08-09-2012
This can be defeated by redirecting into the script a la < /dev/null.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to detect url in use in a script?

Hello, I have a small script and it runs from web application in below format: pipe:///path_to_myscript.sh url1 url2 url3 myscript.sh: #!/bin/bash count=0 while do count=$((count+1)) exec 3>&1 ((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \ while read LINE; do echo... (9 Replies)
Discussion started by: baris35
9 Replies

2. HP-UX

Script to detect time drift

Hello there, I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms. I have been googling a lot but thought to post it in this forum to get a... (17 Replies)
Discussion started by: Green_Star
17 Replies

3. UNIX for Advanced & Expert Users

A script to detect system type

Hi forum, So I am trying to determine the OS type with the following script: #!/usr/bin/sh OStype1=`uname -s` Sunos1=SunOs if then echo "This system is Linux" exit 0 elif then echo "This system is SunOs" exit 0 elif (1 Reply)
Discussion started by: dampio
1 Replies

4. Shell Programming and Scripting

Help with detect with regex and move script

Hi all, I am needing some help with a script that will search for a video file by known extensions and then do a pattern search (I'm guessing via regex) and then based on a match of one type of another move the file to an assigned directory. I would like to do this with either a shell script... (7 Replies)
Discussion started by: Simplify
7 Replies

5. Shell Programming and Scripting

Need to write a shell script that starts one, then kills it, then starts another?

This is on a CentOS box, I have two scripts that need to run in order. I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours (that I specify of course), and then calls the second script (they can't run simultaneously) which... (3 Replies)
Discussion started by: btramer
3 Replies

6. Homework & Coursework Questions

When I run the script, the cursor starts on the wrong line?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: It's a shell script using a looping logic, trap, tput, if, while. Most of the scripts in this book aren't written... (2 Replies)
Discussion started by: ckleinholz
2 Replies

7. UNIX for Advanced & Expert Users

Script to rename file that was generated today and which starts with date

hello, can someone please suggest a script to rename a file that was generated today and filename that being generated daily starts with date, its a xml file. here is example. # find . -type f -mtime -1 ./20130529_4995733057260357019.xml # this finename should be renamed to this format.... (6 Replies)
Discussion started by: bobby320
6 Replies

8. Shell Programming and Scripting

Start script when a user starts a remote session

Howdy, I'm fairly new at bash scripting, but (for some reason) I've been tasked with building a bastion server and logging all (ssh/telnet) remote activity. Each session must create a unique log file - the name of each file must include the user ID, the connection method (ssh/telnet), the name... (2 Replies)
Discussion started by: kilo90
2 Replies

9. UNIX for Dummies Questions & Answers

Why do a unix script starts with a comment?

For eg: #!/usr/bin/ksh <remaining code goes here> .. .. Does the compiler ignores that? Thanks (2 Replies)
Discussion started by: ajincoep
2 Replies

10. UNIX for Dummies Questions & Answers

how to detect my script is already running

I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job. In my script can i have logic to say if this script is already running from the previous cron, then exit. How do i go about doing that. If you describe the... (11 Replies)
Discussion started by: rmulchandani
11 Replies
Login or Register to Ask a Question