# Problems with portupdater script



## babyz (Jan 26, 2013)

Hey guys, I found the portupdater script (DutchDaemon) -> http://forums.freebsd.org/showthread.php?p=39092#post39092
But I don't understand how to use it (and use it in crontab too)

I downloaded the script and did `chmod +x`, then I put it in /usr/local/sbin but when I try to use it `./portupdater` or `portupdater` it doesn't work saying command not found (yes /usr/local/sbin is in my PATH)or if I type `sh portupdater` it ends with this output:


```
root@freebsd:/usr/local/sbin # sh portupdater
: not found:

Updating portaudit first.

: not found: /usr/local/etc/periodic/security/410.portaudit
: not found:

started at Sat Jan 26 18:39:35 CET 2013

=============== Fetching latest ports snapshop from server. ================

: not found:
portupdater: 78: Syntax error: end of file unexpected (expecting "fi")
```

So, /usr/local/etc/periodic/security/410.portaudit is there and if I run it, it works as expected (but the script says not found :/)

Btw, I'm running it as root and my shell is csh but this doesn't matter I think

What is going wrong?! :/


----------



## chatzki (Mar 31, 2014)

It's been written as prerequisite: you must have ports-mgmt/portmaster and portaudit installed before running the script. Probably, you didn't meet these requirements.


----------



## ghostcorps (May 27, 2014)

Hi guys,

Is this script still considered usable for 10.0? I have been using it on 8.2 for a long time and have upgraded today but as it is no longer available here, I wanted to check if I am safe to use my old one.

Thanks.

edit: looking at the script there are clearly a few things that need to be changed. I am giving it a go but if anyone knows of an updated one please let me know.


----------



## ghostcorps (May 27, 2014)

This is what I changed have so far:

Line  8 : portaudit >> pkg-audit
Line 62: pkg_version -IvL '='  >> pkg version -IvL '='
Line 67: pkg_updating >> pkg updating

The modified lines work fine and running `#portupdater fetch` works perfectly. I am just waiting to see if the cron command will do the same.


/usr/sbin/portupdated.sh

```
#!/bin/sh
hostname=$(hostname)
date=$(/bin/date)

echo "
Updating portaudit first.
"
/usr/local/etc/periodic/security/410.pkg-audit

echo "
Portupdater for ${hostname} started at ${date}


========== Fetching latest ports snapshot from server. ==================
"

if [ $# -lt 1 ]
then
portvar="cron"
else
portvar="fetch"
fi

/usr/sbin/portsnap ${portvar} || exit 1

echo "
========== Updating ports tree with new snapshot. =======================
"
/usr/sbin/portsnap update || exit 1

echo "
============ Cleaning out all obsolete distfiles. =======================
"
/usr/local/sbin/portmaster -y --clean-distfiles || exit 1

if [ ${portvar} = "fetch" ]
then
echo "
Ah, you're actually here. Good.

Running some (possibly) interactive stuff.
"
/bin/sleep 5

echo "
============ Cleaning out stale ports. ==================================
"
/usr/local/sbin/portmaster -s || exit 1
echo "
============ Checking port dependencies. ================================
"
/usr/local/sbin/portmaster --check-depends || exit 1
echo "
============ Cleaning up /var/db/ports. =================================
"
/usr/local/sbin/portmaster --check-port-dbdir || exit 1
fi

echo "
=================== See which ports need updating. ======================
"
/usr/sbin/pkg version -IvL || exit 1

echo "
================= Warnings from /usr/ports/UPDATING. ====================
"
/usr/sbin/pkg updating -d `/bin/date -v-1w +%Y%m%d`

echo "
See /usr/ports/UPDATING for further details.

========== Portupdater done. ============================================
```


----------



## DutchDaemon (May 27, 2014)

This is what I use now, under the PKGNG framework. See if it works for you. Inspect any flags and paths before actually running it. I'm not responsible for you nuking your own installation, or installing 30,000 ports.

Note that, as before, running it as `portupdater` injects a random sleep of 0-3600 seconds. So you can put it in cron, and it will mail the output to the cron owner (usually root). If you run it on the command line, add any parameter, e.g. `portupdater yes` or `portupdater a`, and it will run immediately. This also means it will perform interactive tasks, so be prepared to go through the steps.

[ The revised script is four posts down ]


----------



## ghostcorps (May 28, 2014)

Awesome!

Thanks, this works perfectly.


----------



## kpa (May 28, 2014)

Couple of things about the script: You can't assume that /usr/sbin/pkg exists and always works like the real /usr/local/sbin/pkg executable, use /usr/local/sbin/pkg if you want to use full paths. Secondly, `portmaster --check-depends` is redundant, you can just as well use `/usr/local/sbin/pkg check -dn` because that's all what portmaster(8) does in PKGNG mode.


----------



## DutchDaemon (May 28, 2014)

That goes to show it's from early on in the transitional phase. I'll have a look. Thanks.


----------



## DutchDaemon (May 28, 2014)

Right, revised, and some additional prerequisite-checking.


```
#!/bin/sh

! [ -d /usr/ports ] && echo "This works much better with an installed ports tree, run 'portsnap fetch extract' first" && exit 1
! [ -f /usr/local/sbin/pkg ] && echo "You have not installed ports-mgmt/pkg yet." && exit 1
! [ -f /usr/local/sbin/portmaster ] && echo "You have not installed ports-mgmt/portmaster yet." && exit 1

/usr/bin/touch /tmp/lastportupdate
hostname=$(hostname)
date=$(/bin/date)
day=$(/bin/date | /usr/bin/awk '{print $1,$2,$3}')
oldday=$(/bin/cat /tmp/lastportupdate)

echo "
Updating portaudit first.
"
/usr/local/sbin/pkg audit -F

echo "
Portupdater for ${hostname} started at ${date}


========== Fetching latest ports snapshot from server. ==================
"

if [ $# -lt 1 ]
then
portvar="cron"
else
portvar="fetch"
fi

/usr/sbin/portsnap ${portvar} || exit 1

echo "
========== Updating ports tree with new snapshot. =======================
"
/usr/sbin/portsnap update || exit 1
cd /usr/ports && make fetchindex || exit 1

echo "
============ Cleaning out all obsolete distfiles. =======================
"
/usr/local/sbin/portmaster -y --clean-distfiles || exit 1

if [ ${portvar} = "fetch" ]
then
echo "
Ah, you're actually here. Good.

Running some (possibly) interactive stuff.
"
/bin/sleep 5

echo "
============ Cleaning out stale ports. ==================================
"
/usr/local/sbin/portmaster -s || exit 1
echo "
============ Checking port dependencies. ================================
"
/usr/local/sbin/pkg check -dn || exit 1

echo "
============ Cleaning up /var/db/ports. =================================
"
/usr/local/sbin/portmaster --check-port-dbdir || exit 1
fi

echo "
=================== See which ports need updating. ======================
"
/usr/local/sbin/pkg version -ovL '=' || exit 1

echo "
================= Warnings from /usr/ports/UPDATING. ====================
"
weekago=$( /bin/date -v-1w +%Y%m%d )
lastpkg=$( ls -D %Y%m%d -ltr /var/db/pkg | /usr/bin/tail -n1 | /usr/bin/tr -s " " "\t" | /usr/bin/cut -f 6 )
if [ ${weekago} -lt ${lastpkg} ]
 then usedate=${weekago}
 else usedate=${lastpkg}
fi
/usr/local/sbin/pkg updating -d ${usedate}
echo "
See /usr/ports/UPDATING for further details.

========== Portupdater done. ============================================

"

echo "
======================Cleaning out old packages. ========================                                                                                                                                                                    
"    
/usr/local/sbin/pkg clean -y
```


----------



## KdeBruin (May 30, 2014)

Thanks for the updated script.


----------



## chrcol (Sep 26, 2014)

*G*uys if I use portupgrade to upgrade ports is it ok to install portmaster for this script's functions and it wont conflict with portupgrade?


----------



## DutchDaemon (Sep 26, 2014)

No, don't run portmaster and portupgrade together. The latter uses an internal database (pkgdb.db) that is not seen, respected, or updated by portmaster and portmaster uses files to keep track of installed ports that are not used by portupgrade, etc. etc.


----------



## jb_fvwm2 (Sep 26, 2014)

AFAIK it should okay to use both ( aside from the portupdater script ) if one is careful to test and limit their usage to that which works with the new pkg tools,  once the glitches in the latter are fixed on ones' machine and especially if each is upgraded to work more seamlessly with v10 vs v9 in capabilities, syntax etc.  [ And adding a suggestion that someone finds time to reverse engineer and bring upto speed the older portmanager port which has long since been deprecated -- one could cntl-c its initial output and scroll back to find problems...  ]

Thankful for those using this revised script in this thread [ progress ! ] .  Sort of unwilling to as I've found a multi-xterm methodology for updating with the new package tools which aligns more with concurrent usage and is similar to what I had used previously, and under time constraints.


----------



## kpa (Sep 26, 2014)

No seriously do not ever use both for the same purpose. They are absolutely mutually exclusive because the latter uses a separate binary database that is private to ports-mgmt/portupgrade and other tools are not aware of this private database. If you do any modifications to your ports tree or installed packages without ports-mgmt/portupgrade the database will go out of sync and ports-mgmt/portupgrade may not detect the situation and you'll get some very weird problems. Best thing IMO you can do it ditch ports-mgmt/portupgrade alltogether and stick to using ports-mgmt/portmaster only, ports-mgmt/portupgrade was designed to overcome many of the limitations of the old package format, most of those limitations no longer exist because of the PKGNG package format thus making large parts of ports-mgmt/portupgrade's functionality superfluous.


----------

