LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   TCL script runs fine manually but not as a cron job (https://www.linuxquestions.org/questions/linux-newbie-8/tcl-script-runs-fine-manually-but-not-as-a-cron-job-4175433087/)

vsandilya 10-19-2012 12:30 PM

TCL script runs fine manually but not as a cron job
 
Hello

I have a simple TCL script that runs fine manually from the command line but if I schedule it as a cron job it produces only half the output of the script. I can't seem to get why.

The script is in my home directory
/home/user/test.tcl

crontab looks like this
5,10,15,20,25,30,35,40,45,50,55,59 * * * * /home/user/test.tcl > /home/user/hstfiltest

I see the hstfiltest file that updates every 5 mins but not with the complete output

The script starts like this
#!/usr/bin/tclsh
package require expect
{ whole bunch of tcl code}

Any help is appreciated.
Thanks

shivaa 10-19-2012 12:47 PM

Quote:

crontab looks like this
5,10,15,20,25,30,35,40,45,50,55,59 * * * * /home/user/test.tcl > /home/user/hstfiltest
This is neither a problem with crontab, nor with your script, but I think you've made a wrong entry in crontab. You've mentioned:
/home/user/test.tcl > /home/user/hstfiltest, which is wrong.
Your test.tcl script will update the /home/user/hstfiltest all the time and finally it will show you only last executed part of script in the /home/user/hstfiltest.
Solution: Use a >> (double >) between /home/user/test.tcl and /home/user/hstfiltest, so if your script is generating some data, it will keep appending that data in /home/user/hstfiltest until it succesfully gets finish.
Quote:

5,10,15,20,25,30,35,40,45,50,55,59 * * * * /home/user/test.tcl >> /home/user/hstfiltest

mmheera 10-19-2012 12:50 PM

Here are some troubleshooting tips:

http://askubuntu.com/questions/16422...-not-executing

Thanks!

shivaa 10-19-2012 01:04 PM

I'd like to add one more thing, instead of doing:
Quote:

5,10,15,20,25,30,35,40,45,50,55,59 * * * * /home/user/test.tcl >> /home/user/hstfiltest
You should add output file i.e. >> /home/user/hstfiltest in script itself, not in crontab! Just add ">> /home/user/hstfiltest" after the line where your command generates output in your script.
Let's say, your script will look like this:
Code:

for i in $file
do
<command> >> /home/user/hstfiltest
done


vsandilya 10-19-2012 01:34 PM

Quote:

Originally Posted by meninvenus (Post 4810118)
This is neither a problem with crontab, nor with your script, but I think you've made a wrong entry in crontab. You've mentioned:
/home/user/test.tcl > /home/user/hstfiltest, which is wrong.
Your test.tcl script will update the /home/user/hstfiltest all the time and finally it will show you only last executed part of script in the /home/user/hstfiltest.
Solution: Use a >> (double >) between /home/user/test.tcl and /home/user/hstfiltest, so if your script is generating some data, it will keep appending that data in /home/user/hstfiltest until it succesfully gets finish.

Hello. Thanks for replying. I did try the >> option in crontab but that did not help. It still puts nothing out to the file. But I do see the file updates every 5 minutes but nothing is in the file. But, when I run it as a command, the file gets populated.
[user@user ~]$ /home/user/test.tcl >> /home/user/hstfiltest

shivaa 10-19-2012 03:07 PM

As I said in my last reply, add the line ">> /home/user/hstfiltest" in script itself, not in cron, somewhat like this:
Code:

for i in $file
do
<command> >> /home/user/hstfiltest
done

I tested the same with a test script and it's working, so make a try with this.
Further, it would be better if you mention what's your script is, or any illustration please...

vsandilya 10-19-2012 04:12 PM

Quote:

Originally Posted by meninvenus (Post 4810202)
As I said in my last reply, add the line ">> /home/user/hstfiltest" in script itself, not in cron, somewhat like this:
Code:

for i in $file
do
<command> >> /home/user/hstfiltest
done

I tested the same with a test script and it's working, so make a try with this.
Further, it would be better if you mention what's your script is, or any illustration please...

Here is my TCL script. Its a simple script to interact with a modem and find out the state and depending on it do something.

#!/usr/bin/tclsh
package require Expect
set modem "192.168.13.31"
set timeout 30
log_user 0
proc state {arg} {
if {$arg == 1} {
puts "Modem will reboot"
set systemTime [clock seconds]
puts [clock format $systemTime -format {%Y,%m,%d %H:%M:%S}]
send "atz\r"
expect "\nOK"
exit
#send "^]\r"
#expect "\ntelnet>"
#send "q\r"
}
if {$arg == 0} {
puts "Modem is ready"
exit
}
}
spawn telnet $modem 2332
expect "\nPassword: "
send "12345\r"
expect "\nOK"
send "at*netstate?\r"
interact {
-o -re
"\n.*Ready.*" {[state 1]}
"\n.*Dormant.*" {[state 0]}
}

vsandilya 10-22-2012 12:02 PM

Quote:

Originally Posted by meninvenus (Post 4810202)
As I said in my last reply, add the line ">> /home/user/hstfiltest" in script itself, not in cron, somewhat like this:
Code:

for i in $file
do
<command> >> /home/user/hstfiltest
done

I tested the same with a test script and it's working, so make a try with this.
Further, it would be better if you mention what's your script is, or any illustration please...

I took care of printing to the file in my script and got rid of the >> /home/user/testfile in the crontab but still doesn't work.

I don't understand.. Is crontab not recognizing the expect package? If I write a simple TCL script that prints a message to a file, that works but not this...

chrism01 10-22-2012 06:08 PM

cron has a minimal env setting; best to explicitly (ie absolute path) call cmds, files etc.


All times are GMT -5. The time now is 04:32 AM.