Iată un tutorial pas cu pas despre cum să setezi un cron job în cPanel. Cron jobs sunt comenzi automate care rulează la intervale specifice, foarte utile pentru sarcini repetitive, cum ar fi actualizarea unei baze de date, trimiterea de e-mailuri, sau rularea unor scripturi PHP.
Cum să setezi un cron job în cPanel
1. Autentifică-te în cPanel
- Accesează cPanel introducând URL-ul:
http://yourdomain.com/cpanel
- Introdu numele de utilizator și parola pentru contul cPanel.
2. Accesează secțiunea "Advanced"
- După logare, derulează în jos până găsești secțiunea Advanced (Avansat).
- Click pe opțiunea Cron Jobs.
3. Configurarea e-mailului pentru cron jobs (Opțional)
-
În partea de sus a paginii Cron Jobs, vei vedea un câmp pentru Cron Email.
-
Introdu o adresă de e-mail unde dorești să primești notificări despre rularea cron job-urilor (opțional). Dacă nu setezi acest lucru, nu vei primi notificări de fiecare dată când cron job-ul rulează.
- Dacă preferi să nu primești un e-mail după fiecare execuție a cron job-ului, adaugă comanda
>/dev/null 2>&1
la sfârșitul scriptului tău pentru a suprima e-mailurile.
- Dacă preferi să nu primești un e-mail după fiecare execuție a cron job-ului, adaugă comanda
4. Setarea unui nou cron job
Pasul 1: Selectează intervalul de timp
În secțiunea Add New Cron Job (Adaugă un nou cron job), poți selecta intervalul de rulare pentru scriptul tău. Poți seta diferite valori pentru:
- Minute – (0-59)
- Ore – (0-23)
- Ziua lunii – (1-31)
- Luna – (1-12)
- Ziua săptămânii – (0-7) (unde 0 și 7 înseamnă duminică)
Exemple de intervale:
- Pentru a rula cron job-ul la fiecare 5 minute:
*/5 * * * *
- Pentru a rula cron job-ul o dată pe zi, la miezul nopții:
0 0 * * *
- Pentru a rula cron job-ul o dată pe săptămână, duminica, la ora 12:00:
0 12 * * 0
Dacă nu ești sigur cum să configurezi aceste setări, cPanel oferă și opțiuni predefinite, cum ar fi:
- O dată pe minut
- O dată pe oră
- O dată pe zi
- O dată pe săptămână
Pasul 2: Introduce comanda cron
-
În câmpul Command, introdu comanda pe care dorești să o ruleze cron job-ul.
- De exemplu, pentru a rula un script PHP, comanda ar putea arăta astfel:
/usr/local/bin/php /home/username/public_html/script.php
- Înlocuiește
username
cu numele tău de utilizator cPanel șiscript.php
cu numele fișierului PHP pe care vrei să-l rulezi.
- Înlocuiește
- De exemplu, pentru a rula un script PHP, comanda ar putea arăta astfel:
-
Dacă nu știi unde se află PHP pe serverul tău, poți folosi următoarea comandă pentru a afla:
which php
- Aceasta va returna calea către executabilul PHP (de exemplu,
/usr/bin/php
sau/usr/local/bin/php
).
- Aceasta va returna calea către executabilul PHP (de exemplu,
Suprimarea mesajelor cron:
- Dacă nu vrei să primești un e-mail după fiecare rulare a cron job-ului, adaugă această linie la finalul comenzii:
>/dev/null 2>&1
Exemplu de comandă completă pentru un script PHP fără e-mail:
/usr/local/bin/php /home/username/public_html/script.php >/dev/null 2>&1
5. Salvarea cron job-ului
- După ce ai configurat comanda și intervalul de timp, apasă pe butonul Add New Cron Job (Adaugă noul cron job).
- Noul tău cron job va apărea în lista de cron job-uri existente.
6. Verificarea cron job-ului
- Asigură-te că scriptul tău este corect și rulează bine din cron. Dacă ai setat un e-mail pentru notificări, vei primi un mesaj de succes sau eroare la rularea cron job-ului.
- Dacă nu primești e-mailuri și nu adaugi comanda de suprimare
>/dev/null 2>&1
, este posibil să fie o problemă cu scriptul sau comanda.
Exemple comune de cron jobs:
-
Rulare script PHP la fiecare 5 minute:
*/5 * * * * /usr/local/bin/php /home/username/public_html/script.php >/dev/null 2>&1
-
Backup automat zilnic la ora 02:00:
0 2 * * * /usr/local/bin/php /home/username/public_html/backup.php >/dev/null 2>&1
Sfaturi suplimentare:
- Testare: Testează manual scriptul pentru a te asigura că funcționează corect înainte de a-l seta ca cron job.
- Verifică logurile: Dacă cron job-ul nu funcționează cum trebuie, verifică logurile serverului sau folosește opțiunea de e-mail pentru a depista erorile.
Prin acești pași simpli, poți seta și administra cron job-uri direct din cPanel, automatizând sarcinile repetitive pentru site-ul tău sau aplicațiile web.