0

I want to run a python script when booting my NVIDIA Nano Jetson.

The python script prints LSTM generated text to a receipt printer with the following command:

While True:
    textfile = open("test1.txt","w")
    poem = on_epoch_end()
    print(poem)
    textfile.write(poem)
    subprocess.run(["lp","-o", "media=X72MMY2000MM", "test1.txt"])

on_epoch_end() generates the text using Keras.

This works fine when running the script manually, it prints pieces of text continually on the receipt printer. But when I set crontab to run the script at boot with the following code:

@reboot (sleep 80 && cd /home/lauge/Downloads/lstmtextfinal/ && python3 generator.py >> /home/lauge/generator_log.txt &)

Nothing happens. I added the sleep timer to ensure that everything is ready for the script to run. When checking whether the scripts run at startup with:

ps ax | grep generator.py

It shows that both the sleep timer is working, and after the timer has run out, that the script is running in python3. I'm not sure what I'm doing wrong at this point.

Lauge
  • 33
  • 1
  • 6
  • Is the location of the `lp` binary your Python script invokes on your crontab's path? You could try specifying an absolute path to `lp` in your `subprocess.run` call (which you should be able to find by running `command -v lp`) to see if this is the issue. – thmsdnnr Sep 05 '19 at 17:35
  • 1
    You should also capture stderr to the log file (2>&1) for all of the commands in the list. – mao Sep 05 '19 at 17:40
  • removed stderr and stdout pointers to DEVNULL and edited this question accordingly. – Lauge Sep 05 '19 at 19:05
  • I think you need to add the full path to python or other binaries that are not sh builtin commands. – progmatico Sep 05 '19 at 19:11
  • Check the cron logs usually `/var/log/cron/*`, but it may change from environment to environment – geckos Sep 05 '19 at 19:20
  • ~@reboot runs just before rebooting? If so your machine may be rebooting before the cronjob runs? is that possible?~ it runs after reboot – geckos Sep 05 '19 at 19:22
  • Yeah, it may be environment variables missing. So lp may not be in path – geckos Sep 05 '19 at 19:26
  • added full paths for everything, still same problem. – Lauge Sep 06 '19 at 04:27

1 Answers1

0

cron runs with out TTY attached (not as normal login), so you might want to wrap up your code into a shell script and first imitate a normal login and then run your code, e.g. :

cronjob_1.sh:

#!/bin/tcsh
source /home/username/.cshrc
source /home/username/.login
cd ~/tmp
nice time python /home/username/path/to/your/script.py

or

#!/bin/bash
source /home/username/.profile
source /home/username/.bashrc
cd ~/tmp
nice time python /home/username/path/to/your/script.py
  • or whatever is your code, -
    depending on your shell of choice.

And in your crontab – something like this:

18  *   *   *   *   ~username/cronjobs/cronjob_1.sh
Vlad K.
  • 300
  • 3
  • 11