Crontab and interactive script

Hi all,
I'm trying to execute from crontab a script that uses an interactive shell (swmml, Signalware MML Commands).

I think the problem is about the crontab environment which miss tty/console/terminal etc...

After many tryings and searches I didn't come to a solution.

The program is launched from crontab this way:

15 * * * * su - omniusr -c "swmml -e \'DISPLAY-PROCESS\'" > swmml_D_PROCESS.log 2>&1

I always get this error message in the logfile:

The code in swmml (which is a perl script) that cause the error is:

   if (-t STDIN) {
    } elsif (-p STDIN) {
        if (-B STDIN) {
            die 'STDIN is a binary file';
        } else {
            return 1;
        }
    } elsif (-S STDIN) {
        die 'STDIN is a socket';
    } elsif (-c STDIN) {
        die 'STDIN is a character special file';
    } elsif (-f STDIN) {
        if (-B STDIN) {
            die 'STDIN is a binary file';
        } else {
            return 1;
        }
    } else {
        die $baIlaIlI.' doesn\'t know what STDIN is';
    }

Does anybody has a suggestion for this issue?
How can I run such command from crontab?

Thanks for your help!!!

Did you try to run the script "swmml" manually ?

If it is an interactive script, what input does it wait for ?

If you post only a part of the code and not the wholle code, we may miss some things.

Sorry, I didn't explained very well.

Running from command line:

su - omniusr -c "swmml -e \'DISPLAY-PROCESS\'"

works fine and I get a list of processes and then back to the cli.

While when I run it from crontab I get:

ERROR: STDIN is a character special file

I say interactive because I didn't know how to call it ... to me it looks like a problem related to the crontab environment and the nature of the swmml script, which is interactive.

I'm not sure I can send the whole code ...

Thanks,
Evan

1) When you run the script from the command line does the script ask any questions which require keyboard input?

2) When you run the script from the command line, what user are you? root? omniusr? other?

3) Is this a root cron?

1 Like

When I run this:

su - omniusr -c "swmml -e 'DISPLAY-PROCESS'"

from command line I just get some text and then back to cmd line.

I run the above command as root user

Yes I'm working from root crontab

Maybe try making a cron version of the program and comment out the block of code which is testing STDIN.

It's unusual to issue "su - username" rather than "su username" from root cron. This is because the profile for the user may contain commands which are inappropriate for cron (e.g. stty commands). You'd normally create a script containing a preamble to set up a suitable environment to run the program (including a shebang line to get the right shell).

@Evan ,

You missunderstood Methyl and I .

1) First do this :

su - omniusr

2) Now that you are user omniusr, run the script :

swmml -e 'DISPLAY-PROCESS'

3) Are you prompted by the script to manually enter some values ??? if so, which ?

Following your steps I get no prompt at all, it just print the output.

swmml works like perl (it is a perl script): if you want to execute "on the fly" command in perl you write:

perl -e "print \"Hello world\n\""

I used the word "interactive" because if I execute swmml by its own it enters a shell like the following:

-bash-3.00$ swmml

+---------+    Signalware Enhanced Terminal Handler v9.41.1
|  swmml  |    Copyright 1993-2011 Ulticom, Inc.
+---------+    All Rights Reserved

Welcome to swmml. To get some help, type 'man'.
Current editing mode set to: emacs mode.

blahblah:hostname:225 > 

Thanks!

What are you trying to achieve ?
Before scripting something you must ensure that the intermediate commande give the intended result.

Example if you want a script toto.sh that display your hostnae and full id
after having check that

hostname

and

id

give the result you need you can just

echo "I am connected to : $(hostname)"
echo "My ID is : $(id)"

Now if you intend to get output from some swmml command, you first have to ensure that when you run it manually it generate the expected output, you will then be able to start coding the handling of the output according to your needs.

To far if you just want " Hello World " to be Displayed :

echo "Hello World"

would do the work

 perldoc -f -X

Remove the checks for char special and run it, the check was there for a reason, so you'll have to see what breaks and work around it.

Dear all,
it seems that today I can't explain myself very well.
Please forgive me and let me try one last time.
I'll try to start from the beginning:

#1
I need to retrieve some information with the following script:

swmml_D_PROCESS.sh
#!/usr/bin/bash
su - omniusr -c "swmml -e 'DISPLAY-PROCESS'" | /usr/bin/awk -F" " '{if ( $11 ==  "TRUE" || $11 == "FALSE" ) print $11,$3,$1}' > /tmp/swmml_D_PROCESS.tmp
chmod 777 /tmp/swmml_D_PROCESS.tmp

If I execute the script from command line (root user) it works fine and outputs something like the following:

#2
Now I want to launch the swmml_D_PROCESS.sh script in root's crontab:

15 * * * * /scripts/swmml_D_PROCESS.sh > /scripts/swmml_D_PROCESS.log 2>&1

#3
All I get from the above is the following swmml_D_PROCESS.log:

instead of the expected output (#1).

#4
Since the above didn't work I tryed to reduce at minimum the commands being executed, this lead to the "su - omniusr ...." in crontab, but no success.

#5
I tryed one more thing in crontab:

15 * * * * . /etc/profile; /scripts/swmml_D_PROCESS.sh > /scripts/swmml_D_PROCESS.log 2>&1

But nothing changed.

Please, let me know if I miss some informations.

Thanks to all of you for helping me :b:

---------- Post updated at 04:30 PM ---------- Previous update was at 04:29 PM ----------

Bravo Skrynesaver!

Now that we made one step forward can anybody explain why if I launch the script from command line it works while if I put it in crontab it doesn't work?

Maybe something is missing from the crontab environment?

I cannot figure it out.

Thanks Skrynesaver!!!

What's missing is a terminal :wink:
STDIN and STDOUT are character devices rather than ttys when called from cron.

If you modify the script to remove the check it will probably break somewhere else (I don't know the script at all) however it may be possible to isolate where it breaks and resolve the issue using the Expect module or similar, however that may be a complex Perl coding issue

The reason few won't work from CRON but from command line is due to non availability of it's environment from CRON. The same problem I found is with sqlplus which works OK from command line but not from CRON.

For that I have to set my ORACLE_HOME and ORACLE_SID etc explicitly in the script I am executing from cron .

In your case ,
check if the "swmml" env is available to run from cron, if not set it before invoking it.

That is coming from your program. Did you try commenting out that code block because it clearly does not work when run from cron?

Your original version was better. This version runs the script as the wrong user and does not set the environment correctly.
The lines to choose the right Shell and then set up the environment need to be at the beginning of the script /scripts/swmml_D_PROCESS.sh if "su - username" gives trouble.