Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

system call in crontab

by naildownx (Beadle)
on Jul 20, 2009 at 03:33 UTC ( #781513=perlquestion: print w/ replies, xml ) Need Help??
naildownx has asked for the wisdom of the Perl Monks concerning the following question:

I can get my crontab to work...I can my perl script to work in shell and crontab until I add this line,

system("/usr/bin/feh --bg-center /home/user/Pictures/rotate/1.jpg");


It works fine in shell, but produces no results when called by cron...is there a special way I have to help cron call this program for my perl script to be called and work correcly?
The early bird gets the worm but the second mouse gets the cheese.
pretendeavor

Comment on system call in crontab
Download Code
Re: system call in crontab
by Anonymous Monk on Jul 20, 2009 at 03:59 UTC
    What error messages do you get?
      That's what I would like to know...I don't see any when I try to log it..
      The early bird gets the worm but the second mouse gets the cheese.
      pretendeavor
        If you cronjob exit with a non-zero value it typically sends the error message as an email to root. But since you have been getting those emails you probably want to capture the error messages to a file. You can do this my redirecting standard error to a file. Do that that add the string  /tmp/error.txt 2>&1 to your cronjob entry. It would look something like this:
        1 * * * * /usr/whatever/foo > /tmp/error.txt 2>&1
Re: system call in crontab
by graff (Chancellor) on Jul 20, 2009 at 04:23 UTC
    In addition to answering the question about what error message you get (if any), it might also help to know what this means:

    ... I can [get] my perl script to work in shell and crontab until I add this line...

    What exactly did it do before you added that line? (That is, how do you know it worked, and was the addition of that one line really the only change you made?) And what result does it produce when you run it manually from your shell?

    (Do you have any environment variables in your shell that affect the "feh" command? If so, you'll need to set those via %ENV in the perl script, because cron only gives you a minimal environment.)

      It sounds like you understand what I need to do...
      Ok I told perl to do this as a test...send my variable name to a test.txt file and it worked:
      system("echo $pic > test.txt");

      so that is how I know cron is working and it is running the perl script...but I think it has a problem with me running feh...can you elaborate more on how I can set these variables and where?
      The early bird gets the worm but the second mouse gets the cheese.
      pretendeavor
        I don't know anything about what "/usr/bin/feh" does or how it works. I've never heard of it before. But you say you are able to run it (and run the perl script that runs it via "system()") using your shell (though not via cron). So I'm hoping you know what it does.

        If you do the shell command "env" (which will list all your environment variables), do you see anything that seems to relate to this "feh" program (e.g. any variables that have "feh" or "FEH" as part of the variable name)? Is there a man page for "feh" that explains the environment setup for using it?

        If, for example, your normal login-shell environment includes a variable like FEH_CONFIG=... then your perl script just has to include a line like:

        $ENV{FEH_CONFIG} = ...;
        to match whatever is in your shell environment. The point is that cron only runs jobs under a limited shell, leaving out a lot of settings that you might take for granted in your interactive login sessions.

        Just for grins, try running a cron job that just does "env", to see what is in your run-time environment under cron. Or if you like, you could have cron run a perl script like this:

        #!/usr/bin/perl print "$0: Environment\n"; print " $_ = $ENV{$_}\n" for ( sort keys %ENV );
        Comparing that output to what you get from an interactive shell might be informative.
Re: system call in crontab
by cdarke (Prior) on Jul 20, 2009 at 07:20 UTC
    Problems with jobs failing to run in cron are almost always because of the environment. Note: these are cron issues and nothing to do with Perl.

    Shells call start-up files on initialisation, and they call different start-up files depending on:
    a) the shell type, e.g. sh, csh, ksh88, ksh93, bash, behave differently.
    b) if this is a logon shell
    c) if this is an interactive shell

    We have no way of knowing for sure which shell you are using. However, a best guess is that you have a .profile and/or a .kshrc, or maybe .bash_profile and/or .bashrc. If you are using these, then you may need to "source" them before feh will run (that is using the "dot" command : . in sh or ksh, "source" in csh or bash). That would require a shell script containing the "source" command followed by the call to feh.

    It would be better to put the environment settings in your perl script instead, but that would require you to actually know which settings are affecting the feh program.
      In all cases, a script, assuming sh/ksh (& csh for all I know), can determine whether the shell is interactive by testing $- - if it contains the i switch, then it's an interactive shell - which is why the following is commonly seen at the end of a users .profile...
      . . case "$-" in *i*) # Interactive shell exec bash ;; *) # Do nothing of the sort - non-interactive : ;; esac
      Otherwise, any attempt to dot the .profile from a non-interactive shell causes the attempt to hang since the newly exec(1)ed shell will be waiting for input.

      Having said that, any job started by cron will always be run non-interactively - such that if any output i.e. either STDOUT or STDERR, is not redirected, it will be mailed to the crontab(1M)s user.

      I'm rather intrigued as to why the OP is attempting to start a GUI program using cron(1M) - if the intent is to create a background image, then .xinitrc or similar is place to start it or alternatively use the background property of the window manager.

      A user level that continues to overstate my experience :-))
Re: system call in crontab
by JavaFan (Canon) on Jul 20, 2009 at 08:21 UTC
    That command is intended to put an image on the root window of the user running it. However, since a cron job doesn't typically start an X-Windows session, it's a command that doesn't make much sense. If you run it from the "shell", you're actually running it from a pseudo-terminal - try running the command from a console. It won't work then either.
      Thanks for all the help :) I will keep digging and if I ever figure it out I will definitely update the node...Thanks again...I think I am going in the right direction..
      The early bird gets the worm but the second mouse gets the cheese.
      pretendeavor
Re: system call in crontab
by leocharre (Priest) on Jul 20, 2009 at 13:13 UTC

    When the crontab runs it's probably running from the root crontab and therefore running as 'nobody', who may not have permission to do whatever that does.

    Have the script print to STDERR a whoami or to a /tmp/logfile and find out

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://781513]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-10-21 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls