Cron job doing MySQL database back up not working when append date to the file name - cron

I'm using shared cPanel hosting.
When I enter the following cron job command it works:
*/5 * * * * /usr/bin/mysqldump -u user -p'pazzword' my_db > /backup/database.sql
But when I try to add date stamp to the file name, it doesn't generate .sql files.
*/5 * * * * /usr/bin/mysqldump -u user -p'pazzword' my_db > /backup/database_$(date +"%Y_%m_%d").sql
I can't find cron job error logs, have I made an error in my command?

Okay. Finally I managed to fix the problem.
You need to escape % with **** .
So the working cron job command will look like this:
*/5 * * * * /usr/bin/mysqldump -u user -p'pazzword' my_db > /backup/database_$(date +"\%Y_\%m_\%d").sql

Related

CRON job outputting blank lines

I tried to create a CRON job that runs every minute. As a root user, I ran crontab -e, and in the cronjob, I put in my command * * * * * /usr/bin/php {redacted}/index.php > {redacted}/output.txt
the {redacted} file path has permissions added: chmod ogu+rwx -R so everyone should be able to access it. I even created a new user with no sudo or root privileges, and running /usr/bin/php {redacted}/index.php > {redacted}/output.txt obviously writes the output of {redacted}/index.php to {redacted}/output.txt. However, my cron job ends up overwriting and turning the .txt file to just blank, nothing. I have no idea what is going on since I already made sure there were no permission errors, and cron jobs don't seem to have a visible log or output?
I would love to have any ideas about this. I even tried to add the cron job * * * * * root /usr/bin/php {redacted}/index.php > {redacted}/output.txt, all to the same output.
One more thing... I have tried to set the {redacted} location to /var/www/html, /var/www/{a user with sudo perms}, and {/usr/local/bin} all with the same result.
Update:
I tried something as simple as * * * * * php -v > {redacted}/output.txt and * * * * * /usr/bin/php -v > {redacted}/output.txt still with the same result, so it seems like the error is not with requiring root permissions to run my php program

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.

Cron job not getting triggered on Ubuntu

I am trying to schedule a job using cron.
Following are the steps that i did:
sudo vi /etc/crontab
Added the following line :
* * * * * root /bin/ls >> corn_example
Still job is not getting triggered.
I suspect it's because you're not specifying the folder to 'ls' or a folder location for the example file to be create in.
I'd try:
* * * * * root /bin/ls /tmp/ >> /tmp/corn_example

cron job in crontab not working

I have added the following entry:
*/1 * * * * /home/coddict/myapp-dev/spoolemailsender
and the shell that I am trying to execute (the file spoolemailsender) has the following:
#!/bin/sh
php app/console swiftmailer:spool:send --env=dev
Why isn't this script running every 1 minute? Do I need another command to get this cron job running?
You forgot to put user to execute cron job:
*/1 * * * * root /home/coddict/myapp-dev/./spoolemailsender
or
*/1 * * * * root sh /home/coddict/myapp-dev/spoolemailsender
root for example.
Assuming spoolemailsender is executable script and you don't need to do ./spoolemailsender or sh spoolemailsender

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