Crontab with delete before sync - linux

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.

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.

Why is my crontab not working?

I'm trying to get a cron job to run rsync. I started with this:
*/30 * * * * rsync -avz -e "ssh -i /home/ubuntu/ocf_dev_us" ubuntu#10.0.12.76:/home/ubuntu/kumar/ /home/ubuntu/kumar/"
and it wasn't working, so I replaced it with this:
0 * * * * env > /tmp/env.output
and even that isn't working. How do I find out what's going on?
Your cronjob fires only every 60 minutes. Try this
* * * * * env > /tmp/env.output
to find out if your cronjob is working.

Crontab not running a binary file

My crontab file has following:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /bin/bash /root/bin/run1.sh
run1.sh has
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
cd /root/madhu_test/bpstest/
touch bhasina.txt
./bpsh-linux-x86-3.1 appTest_saurav.tcl apptest.ini.96.70.NSSTuned NSS-Tuned
./bpsh-linux-x86-3.1 appTest_saurav.tcl apptest.ini.96.70.S2C CriMajMinS2C
But crontab is unable to run the binary file bpsh-linux-x86-3.1. However touch works.

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