Skip to content

Instantly share code, notes, and snippets.

@vdudouyt
Created March 18, 2014 06:32
Show Gist options
  • Save vdudouyt/9614633 to your computer and use it in GitHub Desktop.
Save vdudouyt/9614633 to your computer and use it in GitHub Desktop.
Programmatically add a cronjob
sub add_cronjob {
my ($spec) = @_;
local *getCmd = sub {
my @chunks = split(/\s+/, $_[0], 6);
return $chunks[5];
};
my $crontext_old = `crontab -l`;
my $existing = grep { getCmd($spec) eq getCmd($_) } split(/\n/, $crontext_old);
return if $existing;
open(CRONTAB, "| crontab -");
print CRONTAB $crontext_old;
print CRONTAB "$spec\n";
close(CRONTAB);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment