2

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?

asked Jul 16, 2016 at 4:43
1
  • 1
    cron executes the scripts using /bin/sh, so the command source is unknown. Use a simple . ( a dot) to include scripts. Commented Jul 16, 2016 at 8:47

2 Answers 2

1

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.

answered Jul 16, 2016 at 5:13
6
  • I have tested sudo crontab -u ubuntu -e but the result still the same. How to put entry in /etc/cron.d? Commented 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. Commented 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 using type, e.g. type roslaunch. Commented Jul 16, 2016 at 5:51
  • 1
    Now it works. It ran but it failed to open the display. Commented 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. Commented Jul 16, 2016 at 19:47
1

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.

answered Jul 17, 2016 at 3:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.