Detect if script starts from queue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Detect if script starts from queue
# 8  
Old 08-09-2012
Quote:
Originally Posted by Corona688
This can be defeated by redirecting into the script a la < /dev/null.
What do you mean with "redirecting into the script" ? Smilie
# 9  
Old 08-09-2012
what exactly doesn't work?
this works as a stand-alone script - lord.sh
Code:
#!/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'

# 10  
Old 08-09-2012
This appears to work better:

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

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

# 11  
Old 08-09-2012
Quote:
Originally Posted by Lord Spectre
What do you mean with "redirecting into the script" ? Smilie
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.

Last edited by Corona688; 08-09-2012 at 03:25 PM..
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 08-10-2012
Quote:
Originally Posted by Corona688
This appears to work better:

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

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

Thanks Corona, but unfortunately I already use STR & STOUT in my script:
Code:
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...
# 13  
Old 08-10-2012
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.
This User Gave Thanks to Corona688 For This Post:
# 14  
Old 08-16-2012
Corona, sorry if I resume the thread, but finally I had time to test your code.
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:

Code:
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

Last edited by Lord Spectre; 08-16-2012 at 07:34 AM..
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