K
K
kr_ilya2020-04-16 13:59:21
Task Schedulers
kr_ilya, 2020-04-16 13:59:21

How to fix error when running script with cron?

Additional Information
Файл upload.ts содержит
console.log(process.argv);
Рассмотрим несколько ситуаций:
1. При запуске скрипта, находясь в директории root/ командой ts-node /root/autoinst/instAPI/bot/upload.ts

Скрипт запускается, но выдает ошибку Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i @types/node`.
Скрин

5e983429f3d7e559755859.png


2. При запуске скрипта, находясь в директории root/autoinst/instAPI командой ts-node /root/autoinst/instAPI/bot/upload.ts
Скрипт выполняется и возвращает
[
  '/root/.nvm/versions/node/v12.14.1/bin/ts-node',
  '/root/autoinst/instAPI/bot/upload.ts'
]

Всё Ок
Скрин
5e983470d1de0945548957.png


3. Выполним скрипт из директории root/ но командой
ts-node --dir /root/autoinst/instAPI/bot/ /root/autoinst/instAPI/bot/upload.ts

Результат тот же, что в п2. (Если выполнить командой ts-node -s /root/autoinst/instAPI/bot/upload.ts результат будет такой же). Список опций ts-node приложен ниже.
Скрин
5e983544213d7232566720.png


Список опций
5e9839c9a0d57570659243.png


If you create a new cron task
*/5 7-21  * * * /root/.nvm/versions/node/v12.14.1/bin/ts-node --dir /root/autoinst/instAPI/bot/ /root/autoinst/instAPI/bot/upload.ts >> log.log


Then after its execution in the var/mail/root file there will be a letter with the following content:
Subject: Cron <[email protected]> /root/.nvm/versions/node/v12.14.1/bin/ts-node --dir /root/autoinst/instAPI/bot/ /root/autoinst/instAPI/bot/upload.ts >> /root/autoinst/tgBot/logs/cronTurn.log
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <[email protected]>
Date: Thu, 16 Apr 2020 13:50:03 +0300 (MSK)

(node:32420) ExperimentalWarning: The ESM module loader is experimental.
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /root/autoinst/instAPI/bot/upload.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:110:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:241:31)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at Loader.import (internal/modules/esm/loader.js:175:17)


That is, the script is not executed, but returns an error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

How can I fix it?

And further. When run from cron, the ts-node command is not defined, so I specified the full path to the file, namely /root/.nvm/versions/node/v12.14.1/bin/ts-node

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Karasik, 2020-04-16
@vitaly_il1

Try adding
*/5 7-21 * * * cd autoinst/instAPI; /root/.nvm/versions/node/...........

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question