Advertisement
ouija_bh

Run motion as a user process in Ubuntu

Jun 7th, 2024 (edited)
189
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | Software | 0 0
  1. Configuring motion version 4.3.2-1ubuntu0.1 as a user process in Ubuntu 22.04. Commands and scripts refer to $USER (your login) and $HOME which is probably /home/$USER on your system.
  2.  
  3. 1. Install motion.
  4. $ sudo apt install motion
  5.  
  6. 2. Create a directory for motion config.
  7. $ mkdir 775 $HOME/motion
  8.  
  9. 3. Create a directory for motion pictures and video.
  10. $ mkdir 700 $HOME/Dropbox
  11.  
  12. 4. Stop the motion daemon.
  13. $ sudo systemctl stop motion.service
  14.  
  15. 5. Prevent motion running automatically as a daemon.
  16. $ sudo systemctl disable motion.service
  17.  
  18. 6. Create a config file in $HOME/motion/motion.conf. This is the config I use to record one image each second of motion at 640x480 resolution. Feel free to make your own config.
  19.  
  20. ### start of motion.conf ###
  21. daemon off
  22. setup_mode off
  23. log_level 6
  24. target_dir $HOME/Dropbox
  25. # Video device (if you used /dev/video1 before, use /dev/video0 in this version of motion)
  26. videodevice /dev/video0
  27. width 640
  28. height 480
  29. framerate 1
  30. text_left CAMERA1
  31. text_right %Y-%m-%d\n%T
  32. emulate_motion off
  33. threshold 12000
  34. noise_level 32
  35. despeckle_filter EedDl
  36. minimum_motion_frames 1
  37. event_gap 2
  38. pre_capture 0
  39. post_capture 0
  40. picture_output on
  41. picture_filename %Y%m%d%H%M%S
  42. movie_output off
  43. webcontrol_port 8080
  44. webcontrol_localhost on
  45. webcontrol_parms 0
  46. ### end of motion.conf ###
  47.  
  48. 7. Create a script to start motion process at $HOME/start_motion.sh.
  49.  
  50. ### start of start_motion.sh ###
  51. #!/bin/bash
  52. IMGDIR=$HOME/Dropbox
  53. VARDIR=$HOME/motion
  54. PIDFILE=$VARDIR/motion.pid
  55. LOGFILE=$VARDIR/motion.log
  56. CONFIGFILE=$VARDIR/motion.conf
  57. if [ -f "$PIDFILE" ]; then
  58. pkill -F $PIDFILE
  59. sleep 1
  60. rm $PIDFILE
  61. fi
  62. if [ ! -d "$VARDIR" ]; then
  63. mkdir 775 $VARDIR
  64. fi
  65. if [ ! -f "$CONFIGFILE" ]; then
  66. echo "Config file not found."
  67. exit 1
  68. fi
  69. if [ ! -d "$IMGDIR" ]; then
  70. mkdir 700 $IMGDIR
  71. fi
  72. if [ -f "$LOGFILE" ]; then
  73. rm $LOGFILE
  74. fi
  75. /usr/bin/motion -b -c $CONFIGFILE -p $PIDFILE -l $LOGFILE
  76. ### end of start_motion.sh ###
  77.  
  78. 8. Create a script to stop motion process at $HOME/stop_motion.sh.
  79.  
  80. ### start of stop_motion.sh ###
  81. #!/bin/bash
  82. PIDFILE=$HOME/motion/motion.pid
  83. if [ ! -f "$PIDFILE" ]; then
  84. echo "Not running."
  85. exit 1
  86. fi
  87. pkill -F $PIDFILE
  88. sleep 1
  89. rm $PIDFILE
  90. ### end of stop_motion.sh ###
  91.  
  92. 9. Create a script to restart motion process at $HOME/restart_motion.sh.
  93.  
  94. ### start of restart_motion.sh ###
  95. #!/bin/bash
  96. $HOME/stop_motion.sh
  97. $HOME/start_motion.sh
  98. ### end of restart_motion.sh ###
  99.  
  100. 10. Make the scripts executable.
  101. $ chmod +x $HOME/start_motion.sh
  102. $ chmod +x $HOME/stop_motion.sh
  103. $ chmod +x $HOME/restart_motion.sh
  104.  
  105. 11. Add your user to the 'video' group.
  106. $ sudo usermod -a -G video $USER
  107.  
  108. 12. Create a startup entry for motion.
  109. 12.1. Run 'Startup Applications' from the menu.
  110. 12.2. Add a program 'Motion' with command '$HOME/start_motion.sh' and delay 5.
  111.  
  112. 13. Start the motion application now. Test your pictures are saved to $HOME/Dropbox. If they are not, motion is being stubborn. Restart the process with $HOME/restart_motion.sh.
  113.  
  114. @version 1.1
Advertisement
Comments
  • ouija_bh
    354 days
    # text 0.21 KB | 0 0
    1. Changes in v1.1:
    2. - Uses systemctl to stop and disable the daemon. To reverse the steps in v1.0:
    3. $ sudo chown root:root $HOME/motion/motion.service
    4. $ sudo mv $HOME/motion/motion.service /lib/systemd/system/
Add Comment
Please, Sign In to add comment
Advertisement