Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions contrib/crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
return get('application', 'application');
});

// Use sudo to run crontab. When running crontab with sudo, you can use the `-u` parameter to change a crontab for a different user.
set('crontab:use_sudo', false);

desc('Sync crontab jobs');
task('crontab:sync', function () {
$cronJobsLocal = array_map(
Expand Down Expand Up @@ -80,6 +83,8 @@

function setRemoteCrontab(array $lines): void
{
$sudo = get('crontab:use_sudo') ? 'sudo' : '';

$tmpCrontabPath = sprintf('/tmp/%s', uniqid('crontab_save_'));

if (test("[ -f '$tmpCrontabPath' ]")) {
Expand All @@ -90,16 +95,18 @@ function setRemoteCrontab(array $lines): void
run("echo '" . $line . "' >> $tmpCrontabPath");
}

run('{{bin/crontab}} ' . $tmpCrontabPath);
run("$sudo {{bin/crontab}} " . $tmpCrontabPath);
run('unlink ' . $tmpCrontabPath);
}

function getRemoteCrontab(): array
{
if (!test("{{bin/crontab}} -l >> /dev/null 2>&1")) {
$sudo = get('crontab:use_sudo') ? 'sudo' : '';

if (!test("$sudo {{bin/crontab}} -l >> /dev/null 2>&1")) {
return [];
}

return explode(PHP_EOL, run("{{bin/crontab}} -l"));
return explode(PHP_EOL, run("$sudo {{bin/crontab}} -l"));
}

12 changes: 11 additions & 1 deletion docs/contrib/crontab.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ return get('application', 'application');
```


### crontab:use_sudo
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L38)

Use sudo to run crontab. When running crontab with sudo, you can use the `-u` parameter to change a crontab for a different user.

```php title="Default value"
false
```



## Tasks

### crontab:sync
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L38)
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L41)

Sync crontab jobs.

Expand Down