Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

perl script in nohup running from crontab

by tanuj (Novice)
on Sep 22, 2011 at 11:49 UTC ( #927335=perlquestion: print w/replies, xml ) Need Help??
tanuj has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i want to run one .pl file in nohup which is configured in crontab. my crontab looks like this: here i am running every minute
[root@perflnx14 SCRIPTS]# crontab -l * * * * * perl /root/nfs_share/SCRIPTS/ >>/root/nfs_share/S +CRIPTS/temp.txt
---------------- script contails:
#!/usr/bin/perl use warnings; use strict; my $cmd='nohup expect -f /root/nfs_share/SCRIPTS/ncp_run.tcl &'; my $tmp= `ps -ef | grep -v grep | grep ncp_run.tcl | wc -l`; if($tmp == 1) { print"already running\n"; } else { print"not running\n"; system $cmd; }
here as you can see if ncp_run.tcl is not running than it will kick start the ncp_run.tcl in nohup __________ but this is not happening , everything is working fine it in the above script i replace $cmd with just another command like date..problem i am facing when i want to start the ncp_run.tcl in nohup

Replies are listed 'Best First'.
Re: perl script in nohup running from crontab
by zentara (Archbishop) on Sep 22, 2011 at 17:37 UTC
    Hi, just some thoughts:

    Googling for "nohup cron scripts" reveals some discussion that nohup is useless in a cron script, because nohup is used when a program is started from a terminal session, and you don't want the program to end when the user closes the terminal. This dosn't happen with cron. There is no terminal for nohup to link to. You might want to try just using & to set it into the background. Try

    my $cmd=" expect -f /root/nfs_share/SCRIPTS/ncp_run.tcl & ";
    nohup may also be causing you trouble from cron by trying to create the nohup.out file, which may be troublesome with cron's restrictions.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      nohup seemed odd to me too, though I've seen it used...

      The ampersand is dubious for the same reason - no terminal, no concept of foreground/background.

Re: perl script in nohup running from crontab
by graff (Chancellor) on Sep 23, 2011 at 02:53 UTC
    A couple things that might be worth trying...
    # crontab -l * * * * * perl /root/nfs_share/SCRIPTS/ >>/tmp/cron_ncp.tmp +.txt 2>&1
    That is, redirect output somewhere else, just to be sure there aren't permission issues, and include stderr together with stdout.
    #!/usr/bin/perl use strict; use warnings; my $tmp = `ps -ef`; if ( $tmp !~ /ncp_run.tcl/ ) { my $cmd = 'expect -f /root/nfs_share/SCRIPTS/ncp_run.tcl'; print "starting now: $cmd\n"; exec $cmd; } printf "ncp_run.tcl is already running as of %s: %s\n", scalar(localti +me), $tmp );
    That is, output a little more information about what's happening, and use exec instead of system (and do a bit of tidying/tightening up).
      Thanks graff,

      cron is executing the script, but now i have a different problem.

      I have all the scripts under /root/nfs_share/SCRIPTS folder as you can see i was trying to run a script ncp_run.tcl (which is again under SCRIPTS dir). In this ncp_run.tcl file i am socurcing some other files

      source resource.conf source init.tcl

      so while corn is executing the script it is throwing error
      couldn't read file "resource.conf": no such file or directory while executing "source "resource.conf"" (file "/root/nfs_share/SCRIPTS/ncp_run.tcl" line 1)
      so is it something like corn is running this in some other environment. here is some of the datails:

      [root@perflnx14 SCRIPTS]# which crontab /usr/bin/crontab [root@perflnx14 SCRIPTS]# echo $SHELL /bin/bash [root@perflnx14 SCRIPTS]# echo $PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/s +bin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/nfs_share/SCRIPTS:/root/n +fs_share/SCRIPTS/ [root@perflnx14 SCRIPTS]# echo $HOME /root

      but if i run this script manually  expect -f ncp_run.tcl </it will work finep> once again thanks for helping me out in this ;-)

        When you log in, your profile is loaded, paths are defined, etc. That does not happen with cron.

        So, when cron'ing, explicitly define the full path for every file you open.

        As indicated by hbm above, the next thing to try is to edit the contents of "nap_run.tcl":
        source /root/nfs_share/SCRIPTS/resource.conf source /root/nfs_share/SCRIPTS/init.tcl
        It's curious that your "nap_run.tcl" file is actually being used/interpreted as a (ba)sh shell script, despite the fact that the ".tcl" extension would lead a casual observer to think that it's actually a tcl script instead. Is it your intention to use the ".tcl" extension on shell scripts? And if "resource.conf" and "init.tcl" don't start with shebang lines, is it your intention that they are also used/interpreted as shell scripts?
Re: perl script in nohup running from crontab
by nimdokk (Vicar) on Sep 22, 2011 at 12:23 UTC
    Can you run the script outside of cron? all by itself from the commandline? Without using Perl?

      ALSO, cron has minimal path provided by a minimal shell, so if it runs on your default shell, it may still not run in cron...

      if the user installed expect or nohup in a dir that's not accessible in cron's shell, you may need to hardpath (provide the full path to every executable run in a script run in cron) them, for them to work within cron.

        i added the full path as /use/bin/expect but still no luck.
      yes I am able to run this script out side the cron. even if the modify the same script $cmd=date and run the same script it will work in corn. so i have some doubt in $cmd='nohup expect -f /root/nfs_share/SCRIPTS/ncp_run.tcl &';

        Any errors in /var/cron/log or wherever you system keeps them? If you can't view that log, add 2>&1 (or whatever is appropriate on your system) to redirect STDERR to your own log file.

        Your cron entry starts with "perl". Why, when you have #!/usr/bin/perl in your script?

        I'd also add #!/path/to/expect -f to the .tcl, to simplify your $cmd.

        And excessive pipe alert in your grep...

        [root@perflnx14 SCRIPTS]# crontab -l * * * * * /root/nfs_share/SCRIPTS/ >>/root/nfs_share/SCRIPT +S/temp.txt ---------------- script: #!/usr/bin/perl use warnings; use strict; #my $cmd='nohup expect -f /root/nfs_share/SCRIPTS/ncp_run.tcl &'; my $cmd='nohup /root/nfs_share/SCRIPTS/ncp_run.tcl &'; #my $tmp= `ps -ef | grep -v grep | grep ncp_run.tcl | wc -l`; my $tmp= `ps -ef | grep -c [n]cp_run.tcl`; if($tmp == 1) { print"already running\n"; } else { print"not running\n"; system $cmd; }
Re: perl script in nohup running from crontab
by nimdokk (Vicar) on Sep 22, 2011 at 16:21 UTC
    Are you trying to run this as root? a regular user or a user with elevated permissions? If you are running as root. I would strongly advise against doing that. Instead have a user with the appropriate permission levels to do the task and nothing more. Running cron jobs as root could be problematic at best (a security issue at worst).
      Thanks for you concern, even i do agree with you doing these kinds of things will open many security issues. but this machine is inside a secure domain , no connection form out side world.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927335]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-09-21 05:47 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (242 votes). Check out past polls.