Doesn't run Cypress & linux cron - linux

I hope for your help. I installed Cypress in Linux and test successfully runs manually on the command cypress run --record --key *******
However, when I write the command to Cron, the test doesn't run. There are no errors in the console. Cron is working. Other commands, such as date and time output, work fine.
I did this:
Created a bash-script.sh with the following content
#!/bin/bash
cd /home/ubuntu/project-name/cypress
/home/ubuntu/project-name/cypress/node_modules/.bin/cypress run --record --key *****************
Put the bash-script file into the folder /home/ubuntu/. This is now the path to the script /home/ubuntu/bash-script.sh
Via the command 'crontab -l ' scheduled the following command */5 * * * * /home/ubuntu/bash-script.sh >> /home/ubuntu/bash-script-log.log
But the scheduled command is not executed. The logs are empty. Can you tell me what I'm doing wrong?

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)

Related

cronjob to run shell script and execute npm command on raspberry pi (as opposed to calling node directly)

I am trying to build a shell script which can be called via a cronjob to trigger an npm nodejs application.
This is my start.sh shell script
#!/bin/bash
#!/usr/local/bin/npm
cd /home/lharby/sites/mysite
npm run start
If I cd to this folder and execute ./start.sh the command appears to run. (Path to bash and npm are both correct after checking which npm).
My cron job looks like this:
*/5 * * * * /home/lharby/sites/mysite/start.sh >> /home/lharby/sites/mysite/src/log/cron-errors.txt 2>&1
This is throwing an error and additionally I was lead to believe that using >> would append to the file, it seems to overwrite it each time.
My guess is that trying to run this command via cron it cannot access certain environment variables that are set up in my index.js
For example:
const config = {
access_token: process.env.NEXT_MASTODON_ACCESS_TOKEN,
client_key: process.env.NEXT_MASTODON_CLIENT_KEY,
client_secret: process.env.NEXT_MASTODON_CLIENT_SECRET,
timeout_ms: 60 * 1000,
api_url: 'https://botsin.space/api/v1/',
};
const M = new Mastodon(config);
I believe I see the same issue when I try to run node index.js from /home/sites/lharby/mysite/src/
As my package.json has this configuration:
"scripts": {
"start": "node ./src/index.js --experimental-modules",
"temp": "node ./src/temp.js --experimental-modules"
},
I was exploring looking at just trying to run the whole app passing in node index.js and passing in the argument flags but I need to be able to run the cron file invoking npm rather than node, as I guess that creates a wrapper and npm can access process.env variables.
From my cron-errors.txt file I am seeing this:
/home/lharby/sites/mysite/node_modules/mastodon-api/lib/mastodon.js:345
throw new Error('Mastodon config must include \'' + reqKey + '\' when using \'user_auth\'');
^
Error: Mastodon config must include 'access_token' when using 'user_auth'
at /home/lharby/sites/glyphbot/node_modules/mastodon-api/lib/mastodon.js:345:27
//
//
/home/lharby/sites/mysite/start.sh: line 4: npm: command not found
/bin/sh: 1: /home/sites/glyphbot/start.sh: not found
/bin/sh: 1: /home/sites/glyphbot/start.sh: not found
How can I ensure the crontab will invoke npm? I feel like I am doing everything correctly.
EDIT
My issues are:
How to run a node project using npm from a cron job?
Can the cronjob access environment variables from the npm command?
Should >> cron-errors.txt 2>&1 append the file rather than replace the content each time.
UPDATE 19.01.23
So I added exports for my variables to the .bashrc file. And when checking echo $NEXT_MASTODON_ACCESS_TOKEN I am seeing my string value.
Updated my .sh file so it now looks like this:
#!/bin/bash
node ./src/index.js --experimental-modules
And updated my cronjob to read this:
*/5 * * * * /home/lharby/sites/mysite/start.sh >> /home/lharby/sites/mysite/src/log/cron-errors.txt 2>&1
And I tried this also trying to bypass the shell script
*/5 * * * * /home/lharby/sites/mysite/src && /usr/local/bin/node index.js --experimental-modules >>/home/lharby/sites/mysite/src/log/cron-errors.txt 2>&1
It still failed with the same message being logged to the cron-errors.txt file.
However I am now able to run this command invoking node with argument flags (rather than using the npm command) So in the terminal I can type
node index.js --experimental-modules
As well as just running the .sh file I just don't understand why it is not passing this information to my cronjob.
I don't understand how if my code reads:
process.env.NEXT_MASTODON_ACCESS_TOKEN,
Will this get replaced or read by the bash export value instead?
Where are your environment variables being set up? It looks like your cron is at least running as expected, and your only problem is to get those env variables into the script correctly. My guess is that you need to use "export" (see bullet point 3)
I would debug this in a few different ways.
you should verify that it is the case that your index.js is not able to read your environment variables. I would recommend adding console.log(JSON.stringify(config, undefined, 2)) to check this.
you should verify that the SHELL has access to those variables before it runs the script. for this just run echo $NEXT_MASTODON_ACCESS_TOKEN (and similar for each variable) to verify if that is the case.
environment variables are a little funny. Assuming you set these values in your .bashrc with NEXT_MASTODON_ACCESS_TOKEN="whateverTheValueIs", just setting variables like this only affects your current process. In order for sub-processes to have the variable you need to export: export NEXT_MASTODON_ACCESS_TOKEN="insertValueHere"
Hope this helps!
Edit:
It's a little unclear what your current issues are, so it might be helpful if you bullet point each issue you are trying to solve

Gitlab job failure: Cannot overwrite variable Host because it is read-only or constant

Hi people of the internet.
Basically I am unable to run even the simplest job and I keep getting the same error no matter what I put in the .gitlab-ci.yml file. See example below:
Here is the .gitlab-ci.yml file:
stages:
- test
job1:
stage: test
tags:
- testing
script:
- echo "Hello world!"
Here is the output ("?" corresponds to intentionally blacked out information):
Running with gitlab-runner 14.10.0 (c6bb62f6)
on runner_test ????????
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on LAPTOP-????????...
Getting source from Git repository
00:01
WriteError:
Line |
219 | $HOST="[MASKED]"
| ~~~~~~~~~~~~~~~~~~~~~~
| Cannot overwrite variable Host because it is read-only or constant.
ERROR: Job failed: exit status 1
I know that $HOST is a reserved variable in powershell but I don't see the link between the error and the code. It may have something to do with the configuration of the runner on Windows. Has anyone encountered this error on Gitlab before? Or any suggestions on how to debug?
Here are the steps that I took to install the runner on Gitlab for Windows (see https://docs.gitlab.com/runner/install/windows.html):
Create a folder somewhere in the system: C:\GitLab-Runner.
Download the binary for 64-bit and put it into the folder (see https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe).
Run prompt as an administrator
Run the following command:
cd C:\GitLab-Runner
gitlab-runner.exe register
Enter your GitLab instance URL (see Gitlab > Settings > CI/CD > Runners > Specific runners)
Enter the token to register the runner (see Gitlab > Settings > CI/CD > Runners > Specific runners)
Enter a description for the runner: runner_test for instance
Enter the tags associated with the runner, separated by commas: testing, windows for instance
Provide the runner executor: shell
Install GitLab Runner as a service and start it
cd C:\GitLab-Runner
gitlab-runner.exe install
gitlab-runner.exe start
I also had to install the latest version of pwsh in Windows (see gitlab-runner: prepare environment failed to start process pwsh in windows):
Run prompt as an administrator
Install the newer pwsh.exe:
winget install Microsoft.PowerShell
Restart the runner
cd C:\GitLab-Runner
gitlab-runner.exe restart
This issue was due to my choice of shell for some reason. A Gitlab runner can choose a shell among the following: bash, sh, powershell, pwsh, and cmd (the last one being deprecated now).
As I stated above I had been using pwsh. So, I went after the config.toml file inside of the C:\GitLab-Runner directory to manually make the change from pwsh to powershell.
...
[[runners]]
name = "runner_test"
executor = "shell"
shell = "powershell"
...
I then restarted the runner and got the job to complete properly:
cd C:\GitLab-Runner
gitlab-runner restart
I still get the error (more like a warning now) but it does not prevent the job from finishing anymore. If anyone has a better answer with a proper explanation I would gladly accept it as the answer to this question.
Note that pwsh to powershell are both powershell scripts (see https://docs.gitlab.com/runner/shells/index.html):
powershell Fully Supported PowerShell script. All commands are executed in PowerShell Desktop context. In GitLab Runner 12.0-13.12, this is the default when registering a new runner.
pwsh Fully Supported PowerShell script. All commands are executed in PowerShell Core context. In GitLab Runner 14.0 and later, this is the default when registering a new runner.

How to get `run_id` when using MLflow Project

When using MLflow Projects (via an MLproject file) I get this message at starting time:
INFO mlflow.projects.backend.local:
=== Running command 'source /anaconda3/bin/../etc/profile.d/conda.sh &&
conda activate mlflow-4736797b8261ec1b3ab764c5060cae268b4c8ffa 1>&2 &&
python3 main.py' in run with ID 'e2f0e8c670114c5887963cd6a1ac30f9' ===
I want to access the run_id shown above (e2f0e8c670114c5887963cd6a1ac30f9) from inside the main script.
I expected a run to be active but:
mlflow.active_run()
> None
Initiating a run inside the main script does give me access the correct run_id, although any subsequent runs will have a different run_id.
# first run inside the script - correct run_id
with mlflow.start_run():
print(mlflow.active_run().info.run_id)
> e2f0e8c670114c5887963cd6a1ac30f9
# second run inside the script - wrong run_id
with mlflow.start_run():
print(mlflow.active_run().info.run_id)
> 417065241f1946b98a4abfdd920239b1
Seems like a strange behavior, and I was wondering if there's another way to access the run_id assigned at the beginning of the MLproject run?
with mlflow.start_run() as run:
print(run.info.run_id)

Jest process quit before all tests execution without any feedback

I am very new to Jest, so I'm trying to run my tests serially using the command yarn jest test -i (same for yarn jest test --runInBand according to Jest documentation).
This is my test files tree:
❯ cd src/test
vipires in scheduling-engine-test/src/test on master
❯ tree
.
├── customer
│ └── clean.test.ts
├── engine
│ └── job.test.ts
└── schedule
├── create.test.ts
└── generateSchedules.test.ts
3 directories, 4 files
The issue is that my project has 4 test files which only one is being tested when I run this command above, it quits occasionally after the first file such as generateSchedules.test.ts
The reason why I need to do it serially is that I'm using Jest for integration tests against an REST API and querying an Oracle Database, so when I run just the command yarn jest test, jest triggers some workers and run all the tests in parallell. It result in some assertion problems because every test file has an beforeAll() and afterAll() methods that delete all my test data generated during test execution.
Below it is my terminal log output for the command I´ve told:
❯ yarn jest test --runInBand
knex:query update "ENGINE_TASK" set "DAT_EXECUTION" = :1 where "IDT_ENGINE_TASK" in (:2, :3, :4, :5, :6) trx1 +0ms
knex:query update "TASK" set "DAT_EXECUTION" = :1 where "IDT_TASK" in (:2, :3, :4, :5, :6) trx1 +63ms
knex:query select * from "ENGINE_TASK" where "IDT_ENGINE_TASK" in (:1, :2, :3, :4, :5) trx3 +165ms
knex:query delete from "ENGINE_TASK" where "IDT_CUSTOMER_ID" = :1 trx5 +6s
knex:query delete from "TASK" "T" where exists (select * from "SCHEDULE" "S" where "IDT_CUSTOMER_ID" = :1 and t.idt_schedule = s.idt_schedule) trx5 +55ms
knex:query delete from "CUSTOMER_EXECUTION_FLAGS" where "IDT_CUSTOMER_ID" = :1 trx5 +70ms
knex:query delete from "SCHEDULE" "S" where "IDT_CUSTOMER_ID" = :1 trx5 +50ms
PASS src/test/schedule/generateSchedules.test.ts (15.647 s)
✓ generate schedules for today (8644 ms)
✓ should block delete on scheduled task (2464 ms)
✓ should permit delete on scheduled task (3173 ms)
RUNS src/test/engine/job.test.ts
vipires in scheduling-engine-test on master took 21s
Notice that the last log line from Jest was RUNS src/test/engine/job.test.ts and then the following line is from my terminal again informing that the execution took 21 seconds.
It was very hard to me to explain that behavior since I wasn't able to find any occurrence of this problem before at Jest documentation nor Stackoverflow. I've tried to discourse about the execution in way to make this post shorten, but anyway if more code is needed to investigate this behavior just let me know.
Node and OS info:
Node version: v14.16.1
Yarn version: v2.4.1
Jest version: 26.6.2
Model Name: MacBook Pro
Model Identifier: MacBookPro 11,4
Processor Name: Quad-Core Intel Core i7
Processor Speed: 2,2 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Hyper-Threading Technology: Enabled
Memory: 16 GB
Boot ROM Version: 199.0.0.0.0
SMC Version (system): 2.29f24

How to run a script in nodeserver every hour?

I have a mean.js project, with this structure in the app (where the server files are):
-app
--controllers
--models
--docs
--logs
--routes
--tests
--views
bower.json
Gruntfile.js
package.json
README.md
I want to do a "cron job" every hour or so on my mongodb.
I ran into this:
https://github.com/scripting/noderunner
Where should i place it and how do i use it let's say for the sake of example,
to do console.log of "hello world" every 1 hour?
first you must install cron to your os, after this you must set cron job, for do this you must write "crontab -e" in console and set job, somthing like this:
0 * * * * /home/user/project/bin/your_script.js
Important! You must add hashbang(#!/usr/bin/env node
) to first line in you script and set permissions. Good luck.

Resources