Skip to content

Script execution terminated when using systemd timer as cron job scheduler #67

Closed

Description

@dbw9580

Steps to reproduce

  1. Use systemd as the cron job scheduler, as per instructions in https://docs.nextcloud.com/server/19/admin_manual/configuration_server/background_jobs_configuration.html#systemd
  2. Set up a workflow script that requires quite some time to run, e.g. sleep 10; echo "finished" > /tmp/flow_output
  3. Trigger the cron job, e.g. from command line systemctl start nextcloudcron.service

Expected behaviour

The workflow script finishes successfully.

Actual behaviour

Nothing happens. No output in /tmp/flow_output.

Workflow Script app

1.4.0

Workaround

Because systemd defaults to kill all the processes of a service unit as soon as the main process has exited, once the php process exits, the child process where the flow script is running will be killed, too. Making systemd only kill the main process but leaving all child processes intact works around this problem (ref: systemd.kill).

Append KillMode=process to the nextcloudcron.service file:

[Unit]
Description=Nextcloud cron.php job

[Service]
User=www-data
ExecStart=/usr/bin/php -f /var/www/nextcloud/cron.php
KillMode=process

I'm not sure if this issue should be filed here, or under the server repo, or the documentation repo. I feel that posting it here may save someone who runs into the same problem hours of researching.

Activity

  1. MorrisJobke commented on Nov 19, 2020

    @MorrisJobke
    Member
  2. kesselb commented on Nov 20, 2020

    @kesselb
    Contributor

    // with wrapping sh around the the command, we leave any redirects in tact,
    // but ensure that the script is not blocking Nextcloud's execution
    $wrapper = 'sh -c ' . escapeshellarg($command) . ' >/dev/null &';
    $this->logger->info(
    'executing {script}',
    ['app' => 'workflow_script', 'script' => $wrapper]
    );
    shell_exec($wrapper);

    The command is executed as subshell on purpose. So the suggest change to the documentation makes sense to me.

  3. blizzz commented on Jan 14, 2021

    @blizzz
    Member

    @dbw9580 thanks for the report, analysis and the documentation PR. Closing hence it is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions