How to properly schedule script autorun as root in Debian

I need to schedule script (create_ap) autorun as root in Debian. I used cron for this (sudo crontab -e). It runs, but doesn’t works. I think this is because its environment is different than one when I run script from regular user with sudo.
So, how to properly schedule autorun of this script?

Asked By: Michael_0208


Just copied essential environment variables to autorun script. Looks like hack but works.

Upd. I need to autorun script at boot as root. I works fine if started manually from regular user with sudo. But it doesn’t work if started by cron (@reboot statement). To find out what’s different I created small script that dumps to file output of these commands: id, env, iwconfig. Ran it 2 times: sudo from user and scheduled by cron. And from these logs found out that cron starts script with truncated environment, PATH looks short, it can’t even find iwconfig. To fix the problem I copied with text editor 2 environment variables (PATH and SHELL) to original script. Script now looks the same, just with 2 additional strings on top:

export PATH=full_path_from_env

export SHELL=/bin/bash

and works fine.

Answered By: Michael_0208
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.