Test a weekly cron job
Just do what cron does, run the following as root
:
run-parts -v /etc/cron.weekly
... or the next one if you receive the "Not a directory: -v" error:
run-parts /etc/cron.weekly -v
Option -v
prints the script names before they are run.
How to run crontab job every week on Sunday
Here is an explanation of the crontab format.
# 1. Entry: Minute when the process will be started [0-60]
# 2. Entry: Hour when the process will be started [0-23]
# 3. Entry: Day of the month when the process will be started [1-28/29/30/31]
# 4. Entry: Month of the year when the process will be started [1-12]
# 5. Entry: Weekday when the process will be started [0-6] [0 is Sunday]
#
# all x min = */x
So according to this your 5 8 * * 0
would run 8:05 every Sunday.
How to unit test node-cron job
Here is the unit testing solution:
server.js
:
const cron = require("node-cron");
const server = (module.exports = {
cronJob: null,
scheduledJob: function(pattern) {
server.cronJob = cron.schedule(pattern, () => {
server.run();
});
},
run: function() {
console.log("run called");
},
});
server.test.js
:
const server = require("./server");
const sinon = require("sinon");
const cron = require("node-cron");
const { expect } = require("chai");
describe("57208090", () => {
afterEach(() => {
sinon.restore();
});
describe("#scheduledJob", () => {
it("should schedule job", () => {
const pattern = "* * * * * *";
const runStub = sinon.stub(server, "run");
const scheduleStub = sinon
.stub(cron, "schedule")
.yields()
.returns({});
server.scheduledJob(pattern);
sinon.assert.calledWith(scheduleStub, pattern, sinon.match.func);
sinon.assert.calledOnce(runStub);
expect(server.cronJob).to.be.eql({});
});
});
describe("#run", () => {
it("should run server", () => {
const logSpy = sinon.spy(console, "log");
server.run();
sinon.assert.calledWith(logSpy, "run called");
});
});
});
Unit test result with 100% coverage:
57208090
#scheduledJob
✓ should schedule job
#run
run called
✓ should run server
2 passing (12ms)
----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
server.js | 100 | 100 | 100 | 100 | |
server.test.js | 100 | 100 | 100 | 100 | |
----------------|----------|----------|----------|----------|-------------------|
You asked for unit testing. If you need an integration testing, please create a new post.
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/57208090
Running aide --check as a crontab job once a week
A user id can only be specified in the system crontab file. The entries of a user's crontab file don't take a user id. The entries in question are apparently found in a user's crontab file, which is why you get root: command not found
from the first, third and fourth entries.
From the second, you get cannot execute binary file
because you ask bash
to execute /usr/sbin/aide
as a bash script when it's not a bash script. You should be using
*/1 * * * * /usr/sbin/aide --check
Crontab Day of the Week syntax
0
and 7
both stand for Sunday, you can use the one you want, so writing 0-6 or 1-7 has the same result.
Also, as suggested by @Henrik, it is possible to replace numbers by shortened name of days, such as MON
, THU
, etc:
0 - Sun Sunday
1 - Mon Monday
2 - Tue Tuesday
3 - Wed Wednesday
4 - Thu Thursday
5 - Fri Friday
6 - Sat Saturday
7 - Sun Sunday
Graphically, * * * * * command to be executed
stands for:
minute | hour | day of month | month | day of week | |
---|---|---|---|---|---|
(0-59) | (0-23) | (1-31) | (1-12) | (1-7) | |
* | * | * | * | * | command to be executed |
Using Cron for a weekly and monthly task
From the conversation we have had in comments, it sounds like your scheduled task is pulling double duty- performing one job each week and then a subtly different job at the end of the month. For that reason (as well as the fact that having scrutinised the documentation, I don't think you can do what you want to do), I would suggest having two different tasks/scripts and running them at the two different times.
Related Topics
Elf Header or Installation Issue with Bcrypt in Docker Container
How to Remove All Lines from a Text File Starting at First Empty Line
How to Use an Older Version of Gcc in Linux
About the Memory Layout of Programs in Linux
How to Activate Programs on Windows from Linux MAChine
Sed Command Working on Command Line But Not in Perl Script
Determining the Path That a Yum Package Installed To
How to Mount One Partition from an Image File That Contains Multiple Partitions on Linux
Linux Commands to Copy One File to Many Files
Delete a Column from a Delimited File in Linux
Insert New Line to Bash Prompts
Searching a Process' Memory on Linux
Cuda Compiler Not Working with Gcc 4.5 +
Makefile Export .O File to a Different Path Than .Cpp