crontab email send - cron

Cron example: */1 * * * * /usr/bin/php /html/includes/CRON.php > /html/includes/logs/CRON_LOG.txt
This is how everything look like, but how add an option not to send me an email's?

You're probably still receiving emails because you're only redirecting stdout, but not stderr. To redirect stderr to /dev/null, use
*/1 * * * * /usr/bin/php /html/includes/CRON.php > /html/includes/logs/CRON_LOG.txt 2> /dev/null
(or wherever you want stderr to be redirected to.

You might define MAILTO in the crontab.
If MAILTO is defined but empty MAILTO="", no mail will be sent.
kind of
MAILTO=""
*/1 * * * * /usr/bin/php /html/includes/CRON.php > /html/includes/logs/CRON_LOG.txt

Add this at the top of the cron file
MAILTO=""

Related

Run yarn script into crontab

I made a script in TypeScript that download data from some api and store inside a mongo DB.
If i run yarn start from the app folder it works well.
I would like to put this command in a cron job that will be executed every 5 minutes.
I try it with some sintax in crontab but ti doesn't work.
I try to put the call in a run.sh script but it doesn't work too.
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts
*/5 * * * * cd /opt/app-folder && /usr/bin/yarn start > /home/username/app-name-out.txt
*/5 * * * * /home/username/run.sh > /home/username/app-name-out.txt
*/5 * * * * /home/username/.nvm/versions/node/v16.15.1/bin/ts-node /opt/app-folder/src/main.ts > /home/username/app-name-out.txt
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts > /home/username/app-name-out.txt
Can someone help me to execute the main.ts every 5 minutes?
Thanks
I get rid of this problem.
There was 2 problems, the first related to the output redirection.
I fixed by redirect stdout in a file and stderr in another one.
The second was related the the $PATH of crontab: it was /usr/bin:/bin.
To fix it I log into my user where script works and I print my $PATH with echo $PATH.
I copied the value and I set it before the crontab line in crontab file.
This is what it looks like:
# Set the same path of user username to have the correct path in script
PATH=/home/username/.nvm/versions/node/v16.15.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
# Execute oracle every 5 minutes
*/5 * * * * /bin/sh /home/username/run.sh >> /home/username/app-name-info.txt 2>> /home/username/app-name-error.txt
Now it works.

Crontab with delete before sync

I have a question, i build on this moment a loadbalancer server with 2 servers. Now i have the sync with crontab.
But if i delete a file or directory on server 1 than stay the file on server 2. and if i delete a file or directory on server 2 than stay the file on server 1.
This my crontab from server 1
SHELL=/bin/bash
HOME=/
*/1 * * * * date >> /var/log/rsync_log
*/1 * * * * rsync -avrhe --delete-before 'ssh -p SSHPORTNUMBER' USERNAME#IPTOSERVER2:/home/ploi/ /home/ploi/ >> /var/log/rsync_log
This my crontab from Server 2
SHELL=/bin/bash
HOME=/
*/1 * * * * date >> /var/log/rsync_log
*/1 * * * * rsync -avrhe --delete-before 'ssh -p SSHPORTNUMBER' /home/ploi/ USERNAME#IPSERVER1:/home/ploi/ >> /var/log/rsync_log
Can anyone help me to fix this problem?
Thanks.
Willem
you can add a shell script , which write delete command, and use crontab to exec it;
when you want to delete a file, you just write command to this sh file, sh file will sync to other server, then use crontab to exec this sh file to delete the real file.

Crontab - simple echo not running

I've got such situation:
I want to schedule a job with crontab on a linux server. I'm not super-user, so I'm editing (with crontab -l, editor vim) only my crontab file. For testing, I put there:
* * * * * echo asdf
And the job is not running. Is the restart of the server needed? Or maybe some administrator move?
May be it is, cron jobs will run in their own shell. So you can't expect to see asdf on your console.
What you should try is
* * * * * echo asdf > somefile_in_your_home_directory_with_complete_path.log
Next check the file by doing a tail:
tail -f somefile_in_your_home_directory_with_complete_path.log
And if it's not, check if the cron daemon itself is running or is down:
# pgrep crond
OR
# service crond status
If you want to echo something on your shell you could use wall:
* * * * * wall <<< "Hello from cron"
* * * * * echo "Hello from cron" | wall
These two lines basically do the same but the first one might not work on older shell, just choose your favorite.
Anyway, be aware that wall will send your message to every user currently connected.
For me * * * * * /bin/echo text > file is not working...I don't know why, previleges and everything is set.
(This command is running normaly when I execute it as the particular
root user, just to clarify this.)
This can be solved by injecting the path PATH=$PATH:/bin in my example.
Instead * * * * * echo text > file is working fine, probably path issue.
Hope I helped

How-To prevent Cronjob from creating a file

I have the following problem:
I have this cronjob:
* 1-23 * * * /usr/bin/wget http://yannick-w.de/Test/getData.php
and this cronjob always creates a new logfile. I don't need this logfile, so I want to prevent cronjob from creating it. How is this possible?
Cheers
Just redirect stdout and stderr, for example
* 1-23 * * * /usr/bin/wget http://yannick-w.de/Test/getData.php > /tmp/getdata.out 2>&1
If no output is produced (e.g. because it has been redirected), it is not logged and not emailed to you by cron
BTW if you don't want any output at all replace /tmp/getdata.out with /dev/null
See crontab(5), cron(8)

Enable/Disable tasks in Crontab by Bash/Shell

Is there a way to enable and disable Crontab tasks using Bash/Shell?
So when the user starts Server 1, it will enable the Server 1 Crontab line and so on.
And when the user stops Server 1, the Server 1 Crontab line get disabled (#).
Is this possible and how?
Thanks in advance
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
SERVERNUM=$1
To enable:
crontab -l | sed "/^#.*Server $SERVERNUM check/s/^#//" | crontab -
To disable:
crontab -l | sed "/^[^#].*Server $SERVERNUM check/s/^/#/" | crontab -
Transcript:
barmar#dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar#dev$ crontab -l | sed '/^[^#].*Server 1 check/s/^/#/' | crontab -
barmar#dev$ crontab -l
#*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar#dev$ crontab -l | sed '/^#.*Server 1 check/s/^#//' | crontab -
barmar#dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
I suggest you add your cron jobs to /etc/cron.d for every server one script. Then let the cron script scan for some marker file if the cron job should be executed.
As a quick and dirty fix, you can enable or disable the execute permission of the appropriate cron script.
E.g. if you like to prevent locate from automatically updating its database (which can be I/O consuming):
cd /etc/cron.daily
sudo chmod a-x locate
This may be against the cron framework, but it is quickly applied and it works in case of immediate needs.
this is a variant, I use a cronjob that loads it self every night. I just edit a file and it gets reloaded at 10pm everynight. You could make the reload happen more often. I keep a directory of files for each of nodes. The trick is make sure that nobody comments out the reload line.
0 22 * * * crontab /home/ME/cron_files/NODE

Resources