How add to crontab sphinx index? - centos

I added an line to crontab file:
*/5 * * * * indexer --config /etc/sphinx/sphinx.conf --all --rotate
But it does not work!
İ tried:
$crontab -l
0 * * * * /etc/sphinx/indexer.sh
*/5 * * * * /etc/sphinx/searchd.sh
indexer.sh
#!/bin/sh
/usr/bin/indexer --config /etc/sphinx/sphinx.conf --all --rotate > /dev/null
searchd.sh
#!/bin/sh
runing=`ps ax | grep "searchd" | grep -v "grep" | wc -l`
if [ $runing -eq 0 ]; then
/usr/bin/searchd --config /etc/sphinx/sphinx.conf
fi

If I could comment, this would be a comment ...
based on the information you provided it is hard to guess what is going wrong. My first guess would be a permission problem. Did you run
crontab -e
with a user with sufficient permissions to run the indexer script? Does the script run when triggered manually?

Related

Unable to set a cron job to take backup of the postgresql database

I have tried to backup postgresql database by using many methods but unable to do.Please help me.
/etc/crontab
0 6 * * * sudo pg_dump -U USERNAME -h REMOTE_HOST -p REMOTE_PORT NAME_OF_DB > LOCATION_AND_NAME_OF_BACKUP_FILE
.pgpass
localhost:5432:db_name:postgres:password
/etc/crontab
0 6 * * * /home/backup.php
backup.php
<?php
exec('pg_dump --dbname=postgresql://username:password#127.0.0.1:5432/mydatabase > dbbackup.sql',$output);
print_r($output);
?>
I tried this as well.
https://dzone.com/articles/linux-database-backup-scheduler-script-postgresql

How to put scheduled restart in CentOS7?

How to create scheduled restart in Centos7, I want to restart my Centos7 server for every day at 4PM
Please try the below command. it will restart the computer in the midle of the night.
shutdown -r 0:00
Same can be done adding the below line /etc/crontab
0 0 * * * /sbin/shutdown -r now
open crontab config file
vim /etc/crontab
Put the following line in the crontab
0 16 * * * /usr/sbin/reboot
Or
crontab -e
put the above line
0 16 * * * /usr/sbin/reboot
and restart crontab
systemctl restart crond.service
contab

How to dump DB via cron inside container?

I use docker-compose which ups a stack.
Relative code:
db:
build: ./dockerfiles/postgres
container_name: postgres-container
volumes:
- ./dockerfiles/postgres/pgdata:/var/lib/postgresql/data
- ./dockerfiles/postgres/backups:/pg_backups
Dockerfile for Postgres:
FROM postgres:latest
RUN mkdir /pg_backups && > /etc/cron.d/pg_backup-cron && echo "00 22 * * * /backup.sh" >> /etc/cron.d/pg_backup-cron
ADD ./backup.sh /
RUN chmod +x /backup.sh
backup.sh
#!/bin/sh
# Dump DBs
now=$(date +"%d-%m-%Y_%H-%M")
pg_dump -h db -U postgres -d postgres > "/pg_backups/db_dump_$now.sql"
# remove all files (type f) modified longer than 30 days ago under /pg_backups
find /pg_backups -name "*.sql" -type f -mtime +30 -delete
exit 0
Cron simply does not launch the script. How to fix that?
FINAL VERSION
Based on #Farhad Farahi answer, below is the final result:
On host I made a script:
#!/bin/bash
# Creates Cron Job which backups DB in Docker everyday at 22:00 host time
croncmd_backup="docker exec -it postgres-container bash -c '/pg_backups/backup.sh'"
cronjob_backup="00 22 * * * $croncmd_backup"
if [[ $# -eq 0 ]] ; then
echo -e 'Please provide one of the arguments (example: ./run_after_install.sh add-cron-db-backup):
1) add-cron-db-backup
2) remove-cron-db-backup'
# In order to avoid task duplications in cron, the script checks, if there is already back-up job in cron
elif [[ $1 == add-cron-db-backup ]]; then
( crontab -l | grep -v -F "$croncmd_backup" ; echo "$cronjob_backup" ) | crontab -
echo "==>>> Backup task added to Cron"
# Remove back-up job from cron
elif [[ $1 == remove-cron-db-backup ]]; then
( crontab -l | grep -v -F "$croncmd_backup" ) | crontab -
echo "==>>> Backup task removed from Cron"
fi
This script adds cron task to host, which launches the script backup.sh (see above) in a container.
For this implementation there is no need to use Dockerfile for Postgres, so relevant part of docker-compose.yml should look like:
version: '2'
services:
db:
image: postgres:latest
container_name: postgres-container
volumes:
- ./dockerfiles/postgres/pgdata:/var/lib/postgresql/data
- ./dockerfiles/postgres/backups:/pg_backups
Things you should know:
cron service is not started by default in postgres library image.
when you change cron config, you need to reload cron service.
Recommendation:
Use docker host's cron and use docker exec to launch the periodic tasks.
Advantages of this approach:
Unified Configuration for all containers.
Avoids running multiple cron services in multiple containers (Better usage of system resources aswell as less management overhead.
Honors Microservices Philosophy.
Based on the Farhad's answer I created a file postgres_backup.sh on the host with the next content:
#!/bin/bash
# Creates Cron Job which backups DB in Docker everyday at 22:00 host time
croncmd_backup="docker exec -it postgres-container bash -c '/db_backups/script/backup.sh'"
cronjob_backup="00 22 * * * $croncmd_backup"
if [[ $# -eq 0 ]] ; then
echo -e 'Please provide one of the arguments (example: ./postgres_backup.sh add-cron-db-backup):
1 > add-cron-db-backup
2 > remove-cron-db-backup
elif [[ $1 == add-cron-db-backup ]]; then
( crontab -l | grep -v -F "$croncmd_backup" ; echo "$cronjob_backup" ) | crontab -
echo "==>>> Backup task added to Local (not container) Cron"
elif [[ $1 == remove-cron-db-backup ]]; then
( crontab -l | grep -v -F "$croncmd_backup" ) | crontab -
echo "==>>> Backup task removed from Cron"
fi
and I added a file /db_backups/script/backup.sh to Docker's Postgres Image with the content:
#!/bin/sh
# Dump DBs
now=$(date +"%d-%m-%Y_%H-%M")
pg_dump -h db -U postgres -d postgres > "/db_backups/backups/db_dump_$now.sql"
# remove all files (type f) modified longer than 30 days ago under /db_backups/backups
find /db_backups/backups -name "*.sql" -type f -mtime +30 -delete
exit 0

Magento - Cronjob Config - hundreds of cron.php.*** files created constantly

A few days ago I set up a cronjob for my Magento 1.9.2.1 Webshop and it works - Emails are processed and sent. But when I look into my root folder hundreds of cron.php.*** (like cron.php.96, cron.php.112, and so on) with zero file size are created all the time.
Does anybody know what I did wrong?
This is the command line I put in via PuTTY SSH:
*/5 * * * * wget -O /dev/null -q http://www.YOURDOMAIN.com/PATH_TO_MAGENTO/cron.php > /dev/null
That's how it worked out for me:
* * * * * /usr/local/bin/php5.4 -f/path/cron.php
You crontab is indeed totally wrong. What wget is doing is indeed trying to access your cron.php via an http request then saves it to a file.
A proper Magento crontab should be edited via the command crontab -u apache-user-name -e, the apache user being something you can find via ps -o "user group command" -C httpd,apache2
And should read :
* * * * * /bin/sh /path/to/your/magento/cron.sh
Reference : http://devdocs.magento.com/guides/m1x/install/installing_install.html#install-cron

sphinx delta search indexing and merge in cron job

I had implemented delta indexing in cron job and which is
*/2 * * * * /usr/bin/indexer --config /etc/sphinx/sphinx.conf indexer sph_idx_posts_delta --rotate > /var/log/cronlog4.log 2>&1
*/3 * * * * /usr/bin/indexer --config /etc/sphinx/sphinx.conf indexer --merge post sph_idx_posts_delta --rotate > /var/log/cronlog5.log 2>&1
both are running perfectly when run in putty but give following error in the log file when run in cron job.
Sphinx 2.2.9-id64-release (rel22-r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinx/sphinx.conf'...
FATAL: there must be 2 indexes to merge specified
I have to do same thing for four more table and all gives me same error. I googled this error and it's saying that create a bash file and than run all code in that file but I am that were also not working, my bash file was not working. any help?
There is redundant part in your commands - indexer, you already specify command in the beginning - /usr/bin/indexer. So, right variant is:
*/2 * * * * /usr/bin/indexer --config /etc/sphinx/sphinx.conf sph_idx_posts_delta --rotate > /var/log/cronlog4.log 2>&1
*/3 * * * * /usr/bin/indexer --config /etc/sphinx/sphinx.conf --merge post sph_idx_posts_delta --rotate > /var/log/cronlog5.log 2>&1