Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

TCL(Tool Command Language) Scripting

Cron with Tcl


Hi,

I am having problems with running a tcl script with Cron. I have read
the past few threads on this topic  on this group but still not able
to solve the problem.

first few lines of the script
==================
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

I have run this script from the command line and tried it in the
following ways:

/home/garima/automation/scripts/Apcon/save_preset.tcl
tclsh /home/garima/automation/scripts/Apcon/save_preset.tcl

This is how my cron looks like:

*/5 * * * *  /usr/bin/mrtg /usr/local/apache2/mrtg/
mrtg.cfg                      <-- this runs
*/2 * * * *  /home/garima/automation/scripts/Apcon/save_preset.tcl

I am a cron newbie and will appreciate any help on this topic.

Thanks,
Garima

What is in your crontab?

in mine I find:
# the shell to run as "sh"
SHELL=/bin/sh
# is your path right to find "tclsh" ?
PATH=/usr/bin:/usr/sbin:/sbin:/bin
# mail uncaptured output to some user of your choosing:
# use this, what ever goes wrong the mailed output should show you!
MAILTO=myself

uwe

Sorry, it was the following all along and may be I changed it
momentarily.

#/usr/bin
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

Even with the above 3 lines it doesn't work :(

[root@vm-linux-1 Apcon]# whereis tclsh
tclsh: /usr/bin/tclsh8.4 /usr/bin/tclsh /usr/share/man/man1/tclsh.1.gz
[root@vm-linux-1 Apcon]#

[root@vm-linux-1 Apcon]# printenv | grep PATH
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/
bin:/usr/X11R6/bin:/home/garima/bin:/usr/local/sbin:/sbin:/bin:/usr/
sbin:/usr/bin
[root@vm-linux-1 Apcon]#

Thanks for your quick response.
-Garima

On May 11, 12:55 pm, Uwe Klein <uwe_klein_habertw@t-online.de>
wrote:

Garima schrieb:

> Sorry, it was the following all along and may be I changed it
> momentarily.

> #/usr/bin
> # the next line restarts using tclsh \
> exec tclsh "$0" "$@"

> Even with the above 3 lines it doesn't work :(

Does the following do the trick?

#/usr/bin/tclsh
# no need to restart - alread running tclsh

--
 => Christian Nassau, http://www.nullhomotopie.de

On May 11, 1:38 pm, Christian Nassau <nore@nullhomotopie.de> wrote:

I just have the following in my script :

#!/usr/bin/tclsh

source /home/garima/automation/library/src

<removed the 2 unwanted lines>

 With whatever I do, I can run it from the command line but the cron
is not running it.

Appreciate all your help!
-Garima

Garima wrote:
> Sorry, it was the following all along and may be I changed it
> momentarily.

your original script was OK


## save_preset.tcl ########################################
#!/usr/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

# your stuff

# end of script
#####################################################

now look into your crontab via "crontab -e"

# crontab ##############################################
SHELL=/bin/sh
# is your path right to find "tclsh" ( you found it in /usr/bin ?
PATH=/usr/bin:/usr/sbin:/sbin:/bin
# mail uncaptured output to some user of your choosing:
# use this, what ever goes wrong the mailed output should show you!
MAILTO=garima

*/5 * * * *  /usr/bin/mrtg /usr/local/apache2/mrtg/mrtg.cfg
                       <-- this runs

*/2 * * * *  /home/garima/automation/scripts/Apcon/save_preset.tcl

# end of crontab
#############################################################

if the PATH shell variable is set as shown above
the second line will  run too.


G!
uwe
Garima wrote:
> Sorry, it was the following all along and may be I changed it
> momentarily.

your original script was OK


## save_preset.tcl ########################################
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

# your stuff

# end of script
#####################################################

now look into your crontab via "crontab -e"

# crontab ##############################################
SHELL=/bin/sh
# is your path right to find "tclsh" ( you found it in /usr/bin ?
PATH=/usr/bin:/usr/sbin:/sbin:/bin
# mail uncaptured output to some user of your choosing:
# use this, what ever goes wrong the mailed output should show you!
MAILTO=garima

*/5 * * * *  /usr/bin/mrtg /usr/local/apache2/mrtg/mrtg.cfg
                       <-- this runs

*/2 * * * *  /home/garima/automation/scripts/Apcon/save_preset.tcl

# end of crontab
#############################################################

if the PATH shell variable is set as shown above
the second line will  run too.


G!
uwe
Great !! After adding the lines you told me to in my crontab I got
pass the first hurdle. Thank you very much!

I am seeking some further assistance from you. In the email I get the
following output: Looks like it is complaining about the
stty -echo. As this is a proc written by someone else, I am not sure
how it will alter the working of the proc if I removed "stty -echo".
But I temporarily removed
it and the script runs completely. Would you know a workaround? I know
I am getting to an expect problem now.

======================OUTPUT====================
date is 14

spawn telnet 10.1.1.10
Trying 10.1.1.10...

Connected to 10.1.1.10.

Escape character is '^]'.

2007-03-22 19:57:15
Thanks for choosing Apcon

login: lab
password:

Apcon CmdX 2.86, Copyright (C) 2004-2006
10.1.1.10>> :
  <parsePrompt> INFO: The prompt is 10.1.1.10
  <connect> INFO: SpawnId is  exp3
  <connect> INFO: prompt is 10.1.1.10

preset num is 14
stty: impossible in this context
are you disconnected or in a batch, at, or cron script?stty:
ioctl(user): bad file number

    while executing
"stty -echo"
    (procedure "sendCommand" line 6)
    invoked from within
"sendCommand $ip_add1 $cmdString"
    (file "/home/garima/automation/scripts/Apcon/save_preset.tcl" line
25)

=========================================================================== ====

On May 11, 1:53 pm, Uwe Klein <uwe_klein_habertw@t-online.de>
wrote:

stty is a unix command _and_ an
equivalent command in provided by expect
with about the same functionality.

your script looks line  an expect script

because of commands like
        stty
        spawn
        expect
        (exp_)send

you will have to either run it with expect
instead of tclsh like this:

#!/bin/sh
# the next line restarts using tclsh \
exec expect "$0" "$@"

or add a
        package require expect

in    your tcl script like this:

#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require expect

source /...

uwe

Looks like the following doesn't do the trick.
I still get the same error.

====================
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Expect

=====================

Error
====
stty: impossible in this context
are you disconnected or in a batch, at, or cron script?stty:
ioctl(user): bad file number

    while executing
"stty -echo"
    (procedure "sendCommand" line 6)
    invoked from within
"sendCommand $ip_add1 $cmdString"
    (file "/home/garima/automation/scripts/Apcon/save_preset.tcl" line
26)

Thanks very much again!

-----------------------------------------

On May 11, 2:34 pm, Garima <garimase@gmail.com> wrote:

in addition to making sure you run with expect, the stty issue is that
when running under cron there is no tty, so trying to operate on it is
a bad idea.

you need to remove the offending code, or do some checking into what
environment you are running under before calling it.

Bruce

could  you post the complete script you are running?

Does your app request a password or other input from
the user when run in interactive mode.

uwe

bruce wrote

> in addition to making sure you run with expect, the stty issue is that
> when running under cron there is no tty, so trying to operate on it is
> a bad idea.

> you need to remove the offending code, or do some checking into what
> environment you are running under before calling it.

One could wrap the whole stuff in another layer of expect ;-)

uwe

On May 11, 3:38 pm, Uwe Klein <uwe_klein_habertw@t-online.de>
wrote:

No there is no interaction with the user. The passwords are hard coded
in the
script. Of course, I am calling procs from other lib files.

Oh well, if using stty is not possible, I might just get rid of it
after one meeting.
I don't even understand what those sttys are doing for us. :-)

#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Expect

source /home/garima/automation/library/src

  set ip_add1 "10.1.1.10"
  set ip_add2 "10.1.1.11"
  set platformType "Apcon"
  set mydate [exec date +%e]

  connect $ip_add1 $platformType -login test  -password test -service
telnet
  connect $ip_add2 $platformType -login test  -password test -service
telnet

  set presetNum 14
  set location "/home/lab/configs/"
  set tmpDate  [eval "exec date" ]

  puts "preset num is $presetNum"
  set cmdString "show preset settings $presetNum"
  sendCommand $ip_add1 $cmdString
  sendCommand $ip_add2 $cmdString

  if {$presetNum == 0 } {
      set presetNum 10
  }

     sendCommand $ip_add1 $cmdString
     sendCommand $ip_add2 $cmdString

   set myhour [exec date +%H]
     set cmdString "set preset current $presetNum"
     sendCommand $ip_add1 $cmdString
     sendCommand $ip_add2 $cmdString
     set destFile [attachTimeStamp $tmpDate $ip_add1]
     set op [sendCommand $ip_add1 "show preset settings $presetNum" ]
     regexp {:\s*([0-9A-Z]*)} $op match var1
     writeBufferFile $var1 $location/Apcon/$destFile

     # Redoing these 4 steps for Apcon Chassis 2

     set destFile [attachTimeStamp $tmpDate $ip_add2]
     set op [sendCommand $ip_add2 "show preset settings $presetNum" ]
     regexp {:\s*([0-9A-Z]*)} $op match var1
     writeBufferFile $var1 $location/Apcon/$destFile

   closeSession $ip_add1
   closeSession $ip_add2

On May 11, 2:50 pm, Uwe Klein <uwe_klein_habertw@t-online.de>
wrote:

> bruce wrote

> > in addition to making sure you run with expect, the stty issue is that
> > when running under cron there is no tty, so trying to operate on it is
> > a bad idea.

> > you need to remove the offending code, or do some checking into what
> > environment you are running under before calling it.

> One could wrap the whole stuff in another layer of expect ;-)

> uwe

You can experiment by calling spawn with the -nottyinit option to
avoid calling stty altogether. I have also
seen in other groups that not expecting eof  can also cause the expect
to "not run".  so
at then of expect script have "expect eof".  People have been running
expect from crontab for years so
this is not the problem. It is more likely that what you are spawning
cannot run without a terminal.
Again a script showing what you are trying to do would help.

Carl

Bezoar wrote:
> this is not the problem. It is more likely that what you are spawning
> cannot run without a terminal.

if you do a spawn in expect the process is connected via a pty pair
which look suspiciously like a tty on the "other" side.

uwe

Garima wrote:

...

> No there is no interaction with the user. The passwords are hard coded
> in the
> script. Of course, I am calling procs from other lib files.

> Oh well, if using stty is not possible, I might just get rid of it
> after one meeting.
> I don't even understand what those sttys are doing for us. :-)

stty -echo turns off character echoing for the terminal, and is used
when inputting passwords typically. Obviously if you are providing the
password in the script then you don't need these stty calls.

> #!/bin/sh
> # the next line restarts using tclsh \
> exec tclsh "$0" "$@"

> package require Expect

> source /home/garima/automation/library/src

>   set ip_add1 "10.1.1.10"
>   set ip_add2 "10.1.1.11"
>   set platformType "Apcon"
>   set mydate [exec date +%e]

As a tip, Tcl has a built-in [clock] command which you could replace all
the calls to [exec date], e.g.:

  set mydate [clock format [clock seconds] -format %e]

-- Neil

On May 11, 12:43 pm, Garima <garimase@gmail.com> wrote:

> I am having problems with running a tcl script with Cron.

I'd suggest solving this problem in steps.  First, get this working

$ cat step1.tcl
#! /bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

puts "script is running"
$

Once the above is running, then you know that any other problems you
are having are later in your tclsh script itself.

On May 11, 3:29 pm, Garima <garimase@gmail.com> wrote:

> Looks like the following doesn't do the trick.
> I still get the same error.
> =====================

> Error
> ====
> stty: impossible in this context
> are you disconnected or in a batch, at, or cron script?stty:
> ioctl(user): bad file number

Read about the Tcl command called "catch". Check, for instance,
http://wiki.tcl.tk/catch . Adding catch around some commands like this
will at least let you get farther.

The stty is a command that interacts with one's keyboard. Of course,
in a cron, there is no keyboard. There are ways to code things so that
these types of errors don't arise, but it sounds to me as if you are a
_user_ of Tcl rather than a developer, so this might be a bit more
than you are ready to handle.

If that's the case, you might need to find out if there is a developer
community for this application, and ask there if anyone could help add
some bullet-proofing for cases like this.

At 2007-05-11 04:02PM, "Garima" wrote:

[... lines that don't reference 'stty' ...]

>    sendCommand $ip_add1 $cmdString

You could show us that src file so we can see what sendCommand is and
how stty is being used.

To me, you need to edit the file containing sendCommand to wrap any stty
calls with expect:

    catch {stty -echo}
    #...
    catch {stty echo}

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc