Try to write the PATH variable at the beginning of the bash script. Just execute echo $PATH; in your terminal/bash and copy the result to make PATH variable as follows.
Let's first make sure that cypress is working in bash and then We will trigger this in crontab.
#!/bin/bash
PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
## Set your cypress folder
yourfullpathtocypressfolder=/home/ubuntu/project-name/cypress;
## Check that we can echo cypress version
$yourfullpathtocypressfolder/node_modules/.bin/cypress -v >> Iamalive.log
After setting variable yourfullpathtocypressfolder Save your script as myscript.sh. try run this script as bash myscript.sh in your terminal.
As for the complete code to record your project using crontab as follows.
Make sure that you are updated your projectID in cypress.json!
You can also check this gist https://gist.github.com/senniksoft/0e062165fb9121be8d8a0fca4038fbc1
#!/bin/bash
PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
## Set your cypress folder
yourfullpathtocypressfolder=/home/ubuntu/project-name/cypress;
## Set your project key
yourprojectkey=xxxxxxxx-c69f-4c44-81c9-xxxxxxxxxxxxx;
cd $yourfullpathtocypressfolder;
## Example Code to record
./node_modules/.bin/cypress run --record --key $yourprojectkey --spec "cypress/integration/examples/actions.spec.js" >> RecordLog.log
After that give proper permissions so that crontab can execute the script.
chmod +x /home/ubuntu/myscript.sh;
Add this script to crontab as follows
*/5 * * * * bash /home/ubuntu/myscript.sh
Example output in the log after running this script.
====================================================================================================
(Run Starting)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Cypress: 5.1.0 │
│ Browser: Electron 83 (headless) │
│ Specs: 1 found (examples/actions.spec.js) │
│ Searched: cypress/integration/examples/actions.spec.js │
│ Params: Tag: false, Group: false, Parallel: false │
│ Run URL: https://dashboard.cypress.io/projects/2bn65e/runs/2 │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: examples/actions.spec.js (1 of 1)
Estimated: 22 seconds
Actions
✓ .type() - type into a DOM element (7441ms)
✓ .focus() - focus on a DOM element (469ms)
✓ .blur() - blur off a DOM element (741ms)
✓ .clear() - clears an input or textarea element (790ms)
✓ .submit() - submit a form (670ms)
✓ .click() - click on a DOM element (2705ms)
✓ .dblclick() - double click on a DOM element (476ms)
✓ .rightclick() - right click on a DOM element (360ms)
✓ .check() - check a checkbox or radio element (1114ms)
✓ .uncheck() - uncheck a checkbox element (1160ms)
✓ .select() - select an option in a <select> element (1068ms)
✓ .scrollIntoView() - scroll an element into view (798ms)
✓ .trigger() - trigger an event on a DOM element (383ms)
✓ cy.scrollTo() - scroll the window or element to a position (2321ms)
14 passing (24s)