Dan Lester

Well-Known Member
Dec 7, 2017
51
1
58
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!
 

rpvw

Well-Known Member
Jul 18, 2013
1,100
477
113
UK
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
 

Dan Lester

Well-Known Member
Dec 7, 2017
51
1
58
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.
 

rpvw

Well-Known Member
Jul 18, 2013
1,100
477
113
UK
cPanel Access Level
Root Administrator
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.
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" [email protected]
 

rpvw

Well-Known Member
Jul 18, 2013
1,100
477
113
UK
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" [email protected]
 

Dan Lester

Well-Known Member
Dec 7, 2017
51
1
58
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.
 

rpvw

Well-Known Member
Jul 18, 2013
1,100
477
113
UK
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)
 

Dan Lester

Well-Known Member
Dec 7, 2017
51
1
58
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" [email protected]" ? That seems to be the right way to chain commands.

Still not sure what the pipe is for.
 

rpvw

Well-Known Member
Jul 18, 2013
1,100
477
113
UK
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
 
Last edited:

Dan Lester

Well-Known Member
Dec 7, 2017
51
1
58
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 [email protected], so you'd like to believe it woudn't be rejected.

Thank you!
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,268
463
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.
 

Dan Lester

Well-Known Member
Dec 7, 2017
51
1
58
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.
 

CPWorld

Member
Jan 31, 2018
5
0
1
Phoenix, AZ
cPanel Access Level
DataCenter Provider
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.
Cpanel really does not do anything other than just simply call the built in cron system in every Linux server and there is really nothing unusual or different about it.

Most all linux systems have two sets of crons where user cron schedules are typically stored in /var/spool/cron.

That folder contains files with the name of the user which contains the schedule for commands to run and while you could in theory execute system commands from the cron schedule directly, it is a bit messy. It is more common to write all your commands to a script file and then call that script file from your cron schedule which there should be an option on your Cpanel control panel to setup unless your hosting provider has taken that option away (often done for security purposes at many hosts).

Assuming you have access to your crontab and again the option would show up on your Cpanel, the first part of what
is written below would then allow you to setup a script that you could run from your hosting account:

For this example, your username is "dan" and you placed your commands in a script file named "myscript.sh" in your home folder,
and you want to run that script every hour at 15 minutes past the top of the hour, the following would go in your crontab using the option in your Cpanel menu (which would physically be stored on the system as "/var/spool/cron/dan"):

Code:
15 * * * * /home/dan/myscript.sh &> /dev/null
As with any Linux shell script, if you are not setting up the script or cron as the "root" user then the script you call containing your commands (myscript.sh in this example) must have executable permissions and be owned by your username.

In this example, the script file named "myscript.sh" was set to permission 750 when uploaded to /home/dan:
Code:
-rwxr-x---.   3 dan dan     496 Dec  7  2017 myscript.sh
As for what to put in the script file itself, well that is whatever commands you want to call:
Code:
#!/bin/bash
cat -- here.txt >> there.txt 
echo "I am done" |  mail -s "Script finished" [email protected]
(by the way, 'cat' is not really meant to copy files. If that is your purpose, use the "cp" command instead)

Another way you could do the above from cron instead of a BASH shell script is to write a PHP script and then you would be able to not only use the built in file copy commands in the PHP language but also the mail() function so your script could directly send email after calling whatever tasks you needed and not rely on the server administrator having installed "mailx".

As a PHP script via a scheduled cron call, your file generally would be setup the same and have the same rules as a normal shell script in terms of permissions and ownership of your script file except that you would call it from your crontab schedule with with either a "php" (scripts) or "GET" (urls) command call from your cron.

Cpanel fully supports setting up crontabs but is not available on all hosting accounts. In fact any hosting provider of shared hosting accounts which allows crontab to be run in itself opens up a major exploitable security hole and it is for this reason that many hosts very often disable the option it does not appear on the Cpanel on many shared web hosting accounts. If you don't have an option to setup your crontab on your control panel, you may not have crontab functions available on your hosting account at all and in that case your best option would be to use a 3rd party remote crontab service to instead call your cron script by URL.

Some hosting providers reduce the security implications by limiting crontab function to their users as something their support sets up for the users. You might need to open a support ticket at your host and ask about their policy on crontabs which it is better in shared hosting to have a host which does not allow crontabs or requires that they setup the crons for the user rather than those which openly allows all users to setup their own crontabs.

Alternatively, you could get a hosting account where you don't have other users on the same server as yourself to worry about and you have more control such as for example a dedicated server or certain classes of VPS or cloud servers where your site(s) are the only accounts active on the server but those options are generally more expensive and overkill if the single only reason at looking at those is strictly for crontab purposes alone.