Knowledgebase / Your Onion Service / Enable cron for your onion service

Enable cron for your hidden service

A Cron service refers to a task scheduler found in Unix-like operating systems. It is utilized by users who establish and upkeep software environments to schedule tasks, referred to as cron jobs, to execute on a regular basis at predetermined times, dates, or intervals.

This guide will illustrate how you schedule a command to run every minute.

You can create cron.php in your www folder, and the PHP script is as below:

<?php
$url = 'https://www.blockonomics.co/api/price?currency=USD';
$proxy = 'proxy:9050';
$timeout = 5;

$ch = curl_init();
if($ch === false) die('Failed to create curl object');

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
$content = curl_exec($ch);
curl_close($ch);

file_put_contents('/var/www/html/price.txt', time() . '.' . $content);

If you set the cron schedule successfully, you will see the file "/var/www/html/price.txt" is changed every minute. And the content will be as follow:

1711723205.{"price": 69666.4}
1711723264.{"price": 69666.4}

On our Customer Portal, click your Onion Service, and navigate to the "Cron" tab.

On our Customer Portal, click your Onion Service, and navigate to the Cron tab.

Scroll to the "Cron Credential" section, you will see your credential to login your cron service.

Scroll to the Cron Credential section, you will see your credential to login your cron service.

Scroll to the "Cron Guideline" section, you will see the guideline to setup your cron service.

Scroll to the Cron Guideline section, you will see the guideline to setup your cron service.

Open Tor Browser, enter your Cron service URL, and enter your credential.

Open Tor Browser, enter your Cron service URL, and enter your credential.

Open Tor Browser, enter your Cron service URL, and enter your credential.

You will see your Cron service with an empty job list.

The Cron service landing page with the environment variables box, the New and Save to crontab buttons and an empty job table

Step 1 — Set the environment variables

Cron does not inherit your shell, so give it one and a PATH. Paste this into the Environment Variables box and press Save to crontab once, before adding any job:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Step 2 — Create the job

Click New, then fill in the command and the schedule. The five fields are the usual cron expression; * * * * * means every minute, and the buttons on the left (Startup, Hourly, Daily…) fill them in for you. Enable error logging writes the output to a log you can read later.

The New job dialog with the command typed in, the quick schedule buttons and the five cron fields set to run every minute

Step 3 — Run the command as the web user

Cron executes the job as root. Everything the job creates — a cache file, a log, a generated image — then belongs to root, while your website runs as www-data. The site keeps working until it needs to write one of those paths again, then it fails with a 500 error or permission denied, and nothing in the portal points at the cron job. Start the command through su and the problem never appears:

su -s /bin/sh www-data -c '/usr/local/bin/php /var/www/html/cron.php'

For a Laravel application the same wrapper runs the scheduler, which fires every task registered in App\Console\Kernel:

su -s /bin/sh www-data -c 'cd /var/www/html && /usr/local/bin/php artisan schedule:run'

If the wrapper answers user www-data does not exist, put the account that owns your writable directories after su -s /bin/sh instead — check it with ls -ld /var/www/html/storage. And if the job calls anything outside the service, keep its output, otherwise a failure leaves no trace:

su -s /bin/sh www-data -c '/usr/local/bin/php /var/www/html/cron.php' >> /var/www/html/storage/logs/cron.log 2>&1

Step 4 — Save it, then write it to the crontab

Save only adds the job to the list in front of you; it is not scheduled yet. Save to crontab, the green button above the list, installs every job of the list into the system crontab — that is the click that starts it. The row shows the command, the schedule and the time of the last change; Edit, Disable and the trash button act on that single job.

The saved job row shows the su-wrapped command, its every-minute schedule and the Edit, Disable and delete actions, with the Save to crontab button above

Export downloads a copy of the crontab, which is worth keeping before a large change.

Step 5 — Verify

Wait for one schedule cycle and check who owns the file the job produced:

ls -l /var/www/html/price.txt

The owner must be www-data — the user your website runs as — and not root. If it says root, the command is missing the su -s /bin/sh www-data -c '...' wrapper.

If a job already ran as root and filled a writable directory with root-owned files, hand back the paths your application writes (adjust the list to what your software uses):

chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

Do not run chown -R over the whole document root as a habit: your application files only need to be readable, and the fewer paths the web user owns, the smaller the damage a compromised script can do.

Tags:
  • Onion Service
  • Cron Schedule

24/7 Expert Support

Our experts are always on hand to help answer your questions, get you started, and grow your presence online. You can email us any time