In Progress CPANEL-41716 - cron_remove api

swbrains

Well-Known Member
Sep 13, 2006
272
37
178
I'm running into some confusion with the cron_remove api function. It appears it wants a "line number" passed in to remove that line from the cron file. So I call fetchcron to get the list and the list returns 3 items, even though I only have two crons showing cpanel. The first is actually a "MAILTO" entry that is empty.

If I loop over the array of returned items and save off the "line" element for the one that has the command I want to delete, in my case it's the third line and its "line" element is indeed "3". If I then call remove_cron with line=3, it fails and says the cron job doesn't exist.

I've noticed the returned data from fetchcron also has a "commandnumber" element for each line -- except for the MAILTO entry which does not have a commandnumber element. My two valid cron entries have a commandnumer of 1 and 2; there is no #3 since the MAILTO entry doesn't have a commandnumber element. Finding the commandnumber for the entry I want to delete and passing that value (2 in my case) to remove_cron seems to work properly, but I don't know if that's the way I'm supposed to call it to delete the proper line.

The docs page for remove_cron that I found says its deprecated, but I was unable to find any other documentation for a newer remove_cron function through searching.

PS: It would seem to make sense to also let remove_cron take the linekey element from an entry, since it's unique for each entry and doesn't require fetchcron combined with looping just to delete a single entry.

Thanks for any clarification you can provide.
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,499
1,971
363
cPanel Access Level
Root Administrator
Hey there! I setup an account with two cronjobs, and confirmed the line numbers are 2 and 3 for those jobs. When it reads /var/spool/cron/username, the first entry there is the SHELL setup, which looks like this on my test account:

Code:
SHELL="/bin/bash"
0 0 * * 0 mail -s "Test of the mail system" [email protected]
* * * * * this is a test
so the line numbers themselves seem to be correct.

I then used https://documentation.cpanel.net/display/DD/cPanel+API+2+Functions+-+Cron::remove_line with this command:

Code:
cpapi2 --user=username Cron remove_line line=3
and got the same behavior as you:

Code:
statusmsg: Cron job not found in the crontab.
so it does seem this is an issue with the folder API call.

In my testing I found that remove_line seems to ignore line one, so if you remove line 2, that removes the expected line. So if you can remember to remove the line number from fetchcron minus 1, it will remove the correct line.

I've created case CPANEL-41716 for our developers to either resolve this or create a newer UAPI call that isn't deprecated, and I'll post updates here as soon as I get more details.