Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

where are cron scripts?

Discussion in 'General Discussion' started by Dan Lester, Jan 7, 2018.

Tags:
  1. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    I'd like a cron job to send me an e-mail when it is complete. I understand that I have to put an

    echo "I'm done"/

    sort of line in my cron script file for that job. But I don't know where the cron script file is! I looked in public_html, and there is nothing there that looks like a cron script. Help!
     
  2. rpvw

    rpvw Well-Known Member

    Joined:
    Jul 18, 2013
    Messages:
    520
    Likes Received:
    159
    Trophy Points:
    43
    Location:
    Spain
    cPanel Access Level:
    Root Administrator
    You might find them in :

    /var/spool/cron/<username>

    But as a website owner, you probably wont have access to these folders.

    You will probably have to modify the script that you are having cron execute for you to include the MAILTO, or use the cPanel Cron Email feature.

    In your cPanel > cron jobs, ensure you have a valid email address in the Cron Email section, and ensure you don't pipe the output of the script to /dev/null in any format.

    Lastly, use the file manager in cPanel to open the file you are calling by cron, and assuming it is a PHP file, insert the following code just before the closing ?> tag
    PHP:
    echo "I'm done";
    Hope this helps
     
  3. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    Thank you. Yes, I guess I don't have access to the cron scripts at all. The cron file is there, but I can't touch it.

    The file I execute with cron isn't a PHP script, but rather just a simple unix command to copy a file from one place into another, as in "cat here >> there". So I assume I can just put that echo statement right there after that command. If that works, that is indeed a very simple way to accomplish this.
     
  4. rpvw

    rpvw Well-Known Member

    Joined:
    Jul 18, 2013
    Messages:
    520
    Likes Received:
    159
    Trophy Points:
    43
    Location:
    Spain
    cPanel Access Level:
    Root Administrator
    I don't think that will work, but you may be able to pipe it as follows
    Code:
    /your/cat here >> there/command | mail -s "Backup Success" admin@example.com
     
  5. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    Pardon me, but I'm kind of naive about this. What is the "your" and "command" for in your suggested script, and what exactly am I piping?
     
  6. rpvw

    rpvw Well-Known Member

    Joined:
    Jul 18, 2013
    Messages:
    520
    Likes Received:
    159
    Trophy Points:
    43
    Location:
    Spain
    cPanel Access Level:
    Root Administrator
    Since you haven't told us the command you are running it was just used as an example - just append the piped bit after your command.

    I am unsure as to why the cPanel cron is not emailing you anyway if you have an valid email in the Cron Email field. The only thing that usually stops an email is not having entered a valid email address in the cPanel cron page, or if you are sending the output of the job to /dev/null

    Note - you may need to use the full path to the mail binary eg
    Code:
     | /bin/mail -s "Backup Success" admin@example.com
     
  7. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    Well, that was the command I was running. "cat here >> there", except I've replaced the filenames for simplicity. That job works fine, as scheduled by cron. It does the job when it is supposed to do the job.

    I'm still a little confused what the command I'm running is piping in your script. I want the echo or mail command to just run. Why do I need something to be piped to it in order to run?

    Yes, I had thought that if I put my e-mail into the cron setup page, it would send me an e-mail confirmation when a cron command had executed. Not so. In fact, it was only when one of my cron commands was accidentally pointing to a file that didn't exist, that I got an error message e-mailed to me. That e-mail seems to be specifically for output produced by the scheduled command. I am not directing the output to /dev/null, and my e-mail is valid.
     
  8. rpvw

    rpvw Well-Known Member

    Joined:
    Jul 18, 2013
    Messages:
    520
    Likes Received:
    159
    Trophy Points:
    43
    Location:
    Spain
    cPanel Access Level:
    Root Administrator
    If you are only getting an email when there is an error, it probably signifies that the command you are using in the cron job does not generate any output under the user you are running it as.

    Try my solution by chaining the mail to the command.

    You may want to read up on Chaining Operators in Linux (Just search for that in your favourite search engine)
     
  9. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    Yes, that's correct. "cat here >> there" doesn't generate any output. So what I learned is that the e-mail address you're giving it isn't where to send a notification, but where to send the output of the executed script.

    Now, about that pipe ...

    Why wouldn't I do "cat here >> there ; mail -s "Backup Success" admin@example.com" ? That seems to be the right way to chain commands.

    Still not sure what the pipe is for.
     
  10. rpvw

    rpvw Well-Known Member

    Joined:
    Jul 18, 2013
    Messages:
    520
    Likes Received:
    159
    Trophy Points:
    43
    Location:
    Spain
    cPanel Access Level:
    Root Administrator
    You can use a semi-colon Operator ( ; ) which will result in a sequential execution of the commands.

    You could use the AND Operator ( && ) which would execute the second command only, if the execution of first command SUCCEEDS

    You can use the PIPE Operator ( | ) where the output of first command acts as an input to the second command so any output generated by your cat should get included in the mail


    I have just tested the PIPE on one of my cron jobs that executes every 5 mins, and it works just fine. Note that the email will probably be sent as user@hostname, and will often be rejected or marked as spam by external mail systems.

    I also got the system email as well from Cron Daemon <root@hostname> with a message body that contained : Null message body; hope that's ok and a subject line which was the Cron <user@host> cron job command
     
    #10 rpvw, Jan 7, 2018
    Last edited: Jan 7, 2018
  11. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    Ah, I see. But my cat command isn't generating any output I want to see. In fact, it's combining one pretty large file with another pretty large file. So I *certainly* don't want it to spit that whole pretty large file back at me! I think the AND operator has to be what I want.

    I'll have to see if the mail is rejected. I'm sending it from mydomain.net to me@mydomain.net, so you'd like to believe it woudn't be rejected.

    Thank you!
     
  12. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    40,819
    Likes Received:
    1,567
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello Dan,

    Note that you may want to seek out help with the coding itself on a website geared more towards development (e.g. StackOverflow). Let us know if you have any additional questions.

    Thank you.
     
  13. Dan Lester

    Dan Lester Active Member

    Joined:
    Dec 7, 2017
    Messages:
    36
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Austin TX
    cPanel Access Level:
    Website Owner
    Yes, this started out as a cPanel-specific question, but turned into a mostly Unix/Linux question. My apologies, but I appreciate the help. There are some peculiarities about cPanel cron (e.g. that it won't act on files not owned by the user), and I'm just starting to resolve them.
     
Loading...

Share This Page