0

I am working with Ubuntu 16.04 on an NVIDIA TX2 system and have a shell script launcher.sh containing the following commands:

#!/bin/sh cd /home/nvidia sudo python test.py >> /home/nvidia/test_output.txt,

basically implying to store the output of test.py in test_output.txt.

The test.pyhas one line: print "Testing startup script!"

I want this script (and more complicated scripts later on) to run automatically when the system boots up. To this end, I added it to the Startup Applications with the command /home/nvidia/launcher.sh and rebooted the system. The file test_output.txt is indeed created but there is no output written in it. I tested the script from the terminal and it works fine.

How do I make this to work from Startup Applications?

Rahul Bohare
  • 762
  • 2
  • 11
  • 31

1 Answers1

2

Init scripts often don't have the complete environment initialized, e. g. PATH. In order to be executed you better provide the complete paths to commands.

On the other hand sudo is unnecessary here, as you obviously don't do anything that needs root permissions. If you need it, be aware that sudo is asking interactively for a password, which stalls the execution of the script, if the command you try to execute with it isn't explictely permitted in /etc/sudoers.

Provided test.py is located in /home/nvidia, and python in /usr/bin, the script should read

#!/bin/sh

cd /home/nvidia
/usr/bin/python test.py >> test_output.txt

And if you want to add error output to the outfile, which may be useful for debugging, make the last line

/usr/bin/python test.py >> test_output.txt 2&>1

or for a separate error log

/usr/bin/python test.py >> test_output.txt 2> test_error.log
Murphy
  • 3,827
  • 4
  • 21
  • 35
  • I replaced `sudo python` with `/usr/bin/python` and it worked perfectly! May I ask why is `2&>1` needed for logging the output? – Rahul Bohare Feb 01 '18 at 12:51
  • 1
    As I wrote it's for redirecting the **error output**: https://stackoverflow.com/a/637838/5794048 – Murphy Feb 01 '18 at 12:57