I am using ubuntu in rapsberry pi 2. I want my script to be ran after I login because it is a GUI program. I have tried to edit /etc/profile and it runs the program before enter the desktop so it cause some of the GUI failed to run. Therefore, I tried to use crontab and it works but I faced some problems.
Here is the line I appended into crontab.
@reboot sh /home/ubuntu/testing.sh >/home/ubuntu/logs/cronlog 2>&1
Here is my script.
#!/bin/bash
source /home/ubuntu/ros_package/devel/setup.bash
roslaunch uvc_camera camera_node.launch &
source /home/ubuntu/catkin_ws/devel/setup.bash
rosrun hybrid_tracking ir_track
Here is my output log.
/home/ubuntu/testing.sh: 3: /home/ubuntu/testing.sh: source: not found
/home/ubuntu/testing.sh: 20: /home/ubuntu/testing.sh: source: not found
/home/ubuntu/testing.sh: 4: /home/ubuntu/testing.sh:
/home/ubuntu/testing.sh: 21: /home/ubuntu/testing.sh: rosrun: not found roslaunch: not found
I have tested my script and it is running in terminal but why there are errors when I run it in cron?
2 Answers 2
This might be due to access right problems.
Your crontab entry must be run with correct user to have access to the home folder of user ubuntu
. So you either add the entry as user ubuntu
doing running crontab
, as root running crontab -u ubuntu -e
, or you can put your entry into a file in /etc/cron.d
with an entry formatted as a normal cronjob, but with an extra user field:
#<timing> <user> <command>
11 * * * * ubuntu /home/ubuntu/testing.sh
Furthermore make shure that your commands are accessible, probably use absolute paths.
-
I have tested
sudo crontab -u ubuntu -e
but the result still the same. How to put entry in/etc/cron.d
?Sam– Sam2016年07月16日 05:42:52 +00:00Commented Jul 16, 2016 at 5:42 -
You should add a file without extension into that directory. Please refer to
man cron(8)
for more information.theldoria– theldoria2016年07月16日 05:48:38 +00:00Commented Jul 16, 2016 at 5:48 -
What's much more important, however, is to use absolute pahts to
roslaunch
and what you else call. Find the absolute path to hem by usingtype
, e.g.type roslaunch
.theldoria– theldoria2016年07月16日 05:51:58 +00:00Commented Jul 16, 2016 at 5:51 -
1Now it works. It ran but it failed to open the display.Sam– Sam2016年07月16日 06:02:29 +00:00Commented Jul 16, 2016 at 6:02
-
I guess you want to start a ros node on boot, maybe using ros-system-daemon-groovy is a way to do it.theldoria– theldoria2016年07月16日 19:47:48 +00:00Commented Jul 16, 2016 at 19:47
I actually found out the answer.
sudo nano /etc/cron.d/anacron
Append this line at the bottom.
@reboot user_name export DISPLAY=:0 && /bin/bash /home/user/testing.sh>/home/user/logs/cronlog 2>&1
My script.
sleep 10
lxterminal -e /home/user/xxx.sh
It can straight away open the script but I use one script to run another script because I need the terminal. Straight away open terminal will face error "cannot open display" in my case. Seem like it haven get ready so I delay 10 seconds and run it.
cron
executes the scripts using/bin/sh
, so the commandsource
is unknown. Use a simple.
( a dot) to include scripts.